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

mvnpdf

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!

Best Answer

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.
Related Question