File Exchange

image thumbnail


version (3.52 KB) by Iman
This function implements Latin hypercube sampling method for correlated random variables.


Updated 07 Mar 2017

View License

This function is developed to generate different realizations of correlated random variables using Latin hypercube sampling method. To learn more about this function, please watch the following tutorial:

Please watch the following short tutorials to quickly learn random sampling (Monte Carlo Vs. Latin hypercube sampling):
Random Sampling - Tutorial 1 - Introduction

Random Sampling - Tutorial 2 - Monte Carlo

Random Sampling - Tutorial 3 - Monte Carlo Example

Random Sampling - Tutorial 4 - Latin Hypercube Sampling

Random Sampling - Tutorial 5 - Monte Carlo Vs. Latin Hypercube Sampling

Please subscribe to my signal processing channel to see more tutorials:

This submission is developed for 2060 Project at the Institute for Integrated Energy Systems at the University of Victoria, Canada. The project has been funded by Pacific Institute for Climate Solutions, Natural Resources Canada and Natural Sciences and Engineering Research Council of Canada. This funding is gratefully acknowledged.

Comments and Ratings (24)


Very useful


Thanks a lot, Amirreza

very useful

Congratulations Iman. I found it very helpful.

I have two questions though.

At line#71 you have: dependent_samples=independent_samples*(P*inv(Q)).';
Why do you multiply with inv(Q)?

The depedent_samples have a correlation pretty close to the defined one(-0.8 in your case), corr(dependent_samples(:,1),dependent_samples(:,2)) but it is different than the correlation of the results, actual_correlation.
Why is this the case

Really, it is good code for helping me.


Hey swelltime. Thank you very much for your feedback. I don't have a citable paper for this as it is common knowledge. You can cite this work as:

Moazzen, Iman (2016). lhsgeneral (, MATLAB Central File Exchange. Retrieved June 9, 2017.


many thanks for this awesome tool! do you have a citable paper or is this common knowledge?


Hi Iman,
it would be even nicer if one could map the latin hypercube to an arbitrary distribution that was given by an input vector! For example, I quite often need one variable to be logarithmic... yet makedist can't provide this.

What's also often needed for my simulations are discrete steps in one dimension. I usually discretize each dimension then so that [] is the number of steps in each dimension. I haven't found a proper way to optimise latin hypercube designs for these cases.


Thank you very much EAnane. You are awesome. I saw some people reporting the bug and wished to get it fixed asap but life has been hectic recently due to the new job and relocation. Anyway, thanks again for your help.


Hi Iman,

thanks a lot for the code. I needed this badly.

@ arnold and I Summer:
I run into the same problem. I resolved it by specifying a 'mu' and 'sigma' for the icdf function. See matlab documentation for icdf
independent_samples(:,i) = icdf('Normal',prob,0,1);
By this you don't get NANs in the R vector.


same error trying to use either of your examples. Matlab version is 2016b
Error using chol
Matrix must be positive definite.

Error in lhsgeneral (line 65)
% Cholesky decomposition of the actual
correlation matrix

I Summer

Hi Iman,

Thank you for creating this code.

I am getting this error:
Error using chol
Matrix must be positive definite.

Error in lhsgeneral (line 70)
Q=chol(R).' ; % Cholesky decomposition of the actual
correlation matrix

I see R is NAN, which depends on the independent samples. Could you help me with this?

Thank you!


Hey Malav. Sorry for the late response. I didn't get your message in my email box which is weird! Could you please send me your MATLAB code? I will take a look at it ASAP. Thank you.

Malav Shah

Iman, I was using your code to generate latin hypercube samples for generelised extreme value distribution. While using your code, it was showing me following error message:

Error using *
Inner matrix dimensions must agree.

Error in lhsgeneral (line 71)
dependent_samples=independent_samples*(P*inv(Q)).'; % transformation matrix which adds
dependency between normal variables

Please help to eliminate this error as soon as possible. Thank you.

Malav Shah

If you can show me for any random variable other than normal, then also that will work. Please reply asap

Malav Shah

Can you please help me in using this code only for one independent distribution? I have only one distribution since there is only one uncertain parameter. I think I don't need to use correlation in case of only one random variable but when I use your code with no correlation, it shows me error. If you can help me in case of using this code only for one distribution i.e. generalized extreme value distribution, then this will be a big help for me. Thank you in advance.

Taotao Zhou


Hi Mimo. Thank you for your positive feedback. The thread is a long page and I am not exactly sure what your question is. My MATLAB function is capable of generating 10 uncorrelated variables with different distribution. Please send me the distribution types, and I will get back to you ASAP. Cheers.


Hi Iman,
Many thanks for providing this useful video tutorials. My issue is I wanna do a LHS sampling of 10 totally UNCORRELATED variables but the variables are all types of different distributions (uniform, normal and lognormal). I saw this example before yours :
Can you advise if this enough for the sampling I aim for? I am just doubted if it is required (and how) to define the variables distribution before performing the sampling or not.



bug fix. Very special thanks to EAnane for finding and fixing the issue!

description update.

description update.

description update.

image update

adding youtube channel

description update.

description update.

Description update.

description update.

title update

link update.

Description Update.

Big update on the algorithm used.

Description Update.

Description update.

Description update.

Description update.

Description update.

Description update.

Videos added.

Title change.

Description Update.

File Update.

Description Update.

MATLAB Release Compatibility
Created with R2013b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor