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

조회 수: 1 (최근 30일)
sogol bandekian
sogol bandekian 2022년 6월 13일
답변: Walter Roberson 2022년 6월 14일
% 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');

답변 (3개)

Benjamin Thompson
Benjamin Thompson 2022년 6월 13일
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 2022년 6월 14일
@Walter Roberson can you please help me for this question

Walter Roberson
Walter Roberson 2022년 6월 14일
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.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics and Visualization에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by