I want to use mvnrnd for generating numbers,but there is error in my code

1 view (last 30 days)
% Multivariate normal random numbers
% Gaussian mean and covariance
d = 31; % number of dimensions
mu = rand(1,d);
sigma = rand(d,d);
sigma = sigma*sigma';
% generate 100 samples from above distribution
N = 100;
Z = mvnrnd(mu, sigma, N);
% plot samples (only for 2D case)
scatter(Z(:,1), Z(:,2), 'filled'),
hold on
x = -3:.2:3;
y = -3:.2:3;
[X,Y]=meshgrid(x ,y);
X = [X Y];
Y=mvnpdf(X,mu,sigma)
Error using mvnpdf
X and MU must have the same number of columns.
Y=reshape(Y,length(x),length(y));
%ezcontour(@(x,y) mvnpdf(X, mu, sigma), xlim(), ylim())
title('Latin Hypercube Sampling')
xlabel('Z_1');
ylabel('Z_2');

Answers (3)

Benjamin Thompson
Benjamin Thompson on 13 Jun 2022
X has 31 columns. mu only has 2 columns since d == 2. Both need to have the same number of olumns. sigma needs to have the same number of columns and rows as the number of columns in X.

sogol bandekian
sogol bandekian on 14 Jun 2022
@Walter Roberson can you please help me for this question

Walter Roberson
Walter Roberson on 14 Jun 2022
X = [X Y];
Before that statement X and Y are each 31 x 31. That statement puts them together to get a 31 x 62
Y=mvnpdf(X,mu,sigma)
mu is 1 x 31 but X is (now) 31 x 62 and that is incompatible size.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!