Generating correlated random numbers
Show older comments
I have 2 independent variables X={x1,x2,...,xn} and Y={y1,y2,...,yn}.
I want to generate a random dependent variable Z={z1,z2,...,zn} that will bear a correlation coefficient Czx with X and Czy with Y.
I found the way to do this for one independent variable X that takes values between 0 and 1:
Z = Czx * rand(n,1) + sqrt(1-Czx^2)*X
It's not clear to me how to do that for two independent variables that both are correlated to Z and can take values from different ranges (e.g. 0<X<1 and 1<Y<10).
Any help will be appreciated.
Answers (1)
I guess I'm a little late here but perhaps some other people might find this response useful. I created this simple function that I used as a workaround.
% code
function [Zv Zs]= randomcorrelatednum(rho)
Zv= rand(1);
Zs = rho*Zv + sqrt(1-rho^2)*rand(1);
while Zv> 1.00000001
Zv= rand(1);
Zs = rho*Zv + sqrt(1-rho^2)*rand(1);
end
end
Categories
Find more on Random Number Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!