The documentation page for randn
shows the following example for generating samples from a bivariate normal distribution:
mu = [1 2];
sigma = [1 0.5; 0.5 2];
R = chol(sigma);
z = repmat(mu,10,1) + randn(10,2)*R
mu
is the mean vector which you should set to a zero vector of appropriate size. sigma
is the covariance matrix, B^-1
in your example. The example above draws 10 samples, you can change this to however many you need. Also remember to change the dimension 2
to N
in your application.
I think this is what you are looking for:
% Number of samples for each variable:
k = 100;
% Your parameters:
mu = [0; 0]; % Vector of Means (0 in your case)
cov = [3 1; 1 3]; % Covariance Matrix (your B)
% Draw the samples...
s = mvnrnd(mu,cov,k);
If you want to perform the same calculation manually (by generating a sample of independent standard normal variables and then applying the appropriate transformation):
% Number of samples for each variable:
k = 100;
% Your parameters:
mu = [0 0]; % Vector of Means (0 in your case)
cov = [3 1; 1 3]; % Covariance Matrix (your B)
% Draw the samples...
s_ind = randn(k,size(cov,1));
s = repmat(mu,k,1) + (chol(cov) * ind_s);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.