Browsed by
Category: Matlab

[Matlab] Generating random numbers with given covariance matrix

[Matlab] Generating random numbers with given covariance matrix

We are given a covariance matrix, and we’d like to generate n vectors, whose coordinates’ covariance matrix is equal to given. We also like each coordinate to have given mean. Here is a code: function numbers = randomcov(n, covMatrix, offset) x=randn(n,length(covMatrix(1,:))); numbers = x*inv(chol(cov(x)))*chol(covMatrix); for i=1:length(offset) numbers(:,i) = offset(i)+numbers(:,i)-mean(numbers(:,i)); end endfunction numbers = randomcov(n, covMatrix, offset) x=randn(n,length(covMatrix(1,:))); numbers = x*inv(chol(cov(x)))*chol(covMatrix); for i=1:length(offset) numbers(:,i) = offset(i)+numbers(:,i)-mean(numbers(:,i)); end end Usage: n – number of vectors to generate covMatrix – given covariance matrix…

Read More Read More