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.
  1 件のコメント
sogol bandekian
sogol bandekian 2022 年 6 月 13 日
I changed d to 31,yet there is still error in running.can you please help more?

サインインしてコメントする。


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 Center および File ExchangeExploration and Visualization についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by