MATLAB: How to generate random matrices from the multivariate normal distribution using the Statistics Toolbox

distributionmultivariatenormalStatistics and Machine Learning Toolbox

I would like to generate random matrices from the multivariate normal distribution using the Statistics Toolbox.

Best Answer

The function mvnrnd.m allows you to generate random matrices from the multivariate normal distribution. This function is included with Statistics Toolbox 3.0 (R12.1) and later versions.
Following is a quick example illustrating MVNRND:
mu1=.5;mu2=1.4;
var1=.9;var2=.6;
cov_desired=.5;
mean_desired=[mu1 mu2]
cov_matrix_desired=[var1 cov_desired;cov_desired var2]
%[R p]=chol(cov_matrix_desired)
% The covariance matrix must be positive definite (pd)
% If p is 0, this is a pd matrix;
% see Solution 1530
M=mvnrnd(mean_desired,cov_matrix_desired,100000);
actual_mean=mean(M)%Close to mean_desired
actual_cov=cov(M)% Close to cov_matrix_desired