Multivariate Normal Distribution with a 27x27 matrix: Error using mvnpdf (line 67)

조회 수: 5 (최근 30일)
Hello everyone,
for the analysis of a measurement I'm trying to use the multivariate normal distribution function mvnpdf.
This is the code I'm using:
X = rand(27,27)
mu = mean(X)
Sigma = cov(X)
x1 = 0:0.1:2.6; x2 = 0:0.1:2.6;
[X1,X2] = meshgrid(x1,x2);
F = mvnpdf([X1(:) X2(:)],mu,Sigma);
I then get the following error message:
Error using mvnpdf (line 67)
X and MU must have the same number of columns.
I don't understand this, since the number of columns is 27 for each of the variables I'm using.
I'm using Matlab R2018a.
Does anyone have an idea why I get this error?
Best regards & thanks for any help!
  댓글 수: 1
John D'Errico
John D'Errico 2018년 10월 6일
"Does anyone have an idea why I get this error?"
Yes. You get an error because you are utterly confused as to what a multi-variate normal distribution means, and how to work with it. In fact, you are so confused, I cannot even figure out what you are really trying to do here, because your code makes no sense at all. You seen to be confusing a two dimensional array of uniform random numbers, with something that would be drawn from a 27 dimensional random variable. And then, even though you started with a uniform sample, you want to work with a multivariate normal PDF. So you don't seem to understand what dimensionality means in this respect. You don't seem to understand what a PDF is. I don't think you understand what mean and cov do for you here. And possibly, you are confused about the difference between uniform and normal random numbers, since you started out using rand, but then tried calling mvnpdf.
These things are not bad. Merely a reflection that you are truly and utterly confused. And in fact, this is not that uncommon, that someone finds themselves confused with these concepts. But in order for someone to help you, they need to know what you are trying to do in the first place.
So, perhaps it would be best to ask what are you trying to do? Explain clearly what you want to do.

댓글을 달려면 로그인하십시오.

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 10월 6일
편집: KALYAN ACHARJYA 2018년 10월 6일
I am suspecting some an issue with this line, as If you check the documentation
F=mvnpdf([X1(:) X2(:)],mu,Sigma);
If you replace this with
F=mvnpdf(X1,mu,Sigma);
It's perfectly working and the error which reflected I don't think its with X and mu, already have equal column numbers.
You have written [X1(:) X2(:)] its return the 729x2
Hope this helps you to debug your code.

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by