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

3 vues (au cours des 30 derniers jours)
sogol bandekian
sogol bandekian le 13 Juin 2022
% 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');

Réponses (3)

Benjamin Thompson
Benjamin Thompson le 13 Juin 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.
  1 commentaire
sogol bandekian
sogol bandekian le 13 Juin 2022
I changed d to 31,yet there is still error in running.can you please help more?

Connectez-vous pour commenter.


sogol bandekian
sogol bandekian le 14 Juin 2022
@Walter Roberson can you please help me for this question

Walter Roberson
Walter Roberson le 14 Juin 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!

Translated by