Error using .* Matrix dimensions must agree.

Hello, I'm trying to build the probability density function (2D) with Matlab. The formula is on the picture below. and then visualize it in 3D.
I'm getting an Error by implementing this function bei Z= .... I'm new in Matlab and need your help
function P=plot_cov(s1,s2,c)
P=[s1^2 s1*s2*c;c*s1*s2 s2^2];
Pinv = inv(P);
x= linspace(-1.6*max(s1,s2), 1.6*max(s1,s2),41);
y= linspace(-1.6*max(s1,s2), 1.6*max(s1,s2),41);
[X,Y]= meshgrid(x,y);
n=2;
Z= 1/((2*3.41)^(n/2)*sqrt(det(P)))*exp((-1/2)*x'.*Pinv.*x) ;
%Graphic
mesh(X,Y,Z);
%surf(X,Y,Z);
%contour(X,Y,Z);
%surf(X,Y,Z,'FaceColor','interp',...
%'EdgeColor','none',...
%'FaceLighting','phong');
xlabel('X');
ylabel('Y');
title('PDF Gauss');
set(gcf,'Name','Gauss ','NumberTitle','off'); end

댓글 수: 2

Image Analyst
Image Analyst 2015년 5월 17일
What is the error? Copy all the red text and paste it back here. Also show how your main program is calling plot_cov() - like what argument values it's passing to it.
corny
corny 2015년 5월 17일
*Error using * Inner matrix dimensions must agree.
Error in visual_gauss1 (line 16) Z= 1/((2*pi)^(n/2)*sqrt(det(P)))*exp((-1/2)*X' * Pinv * X) ;
This is what I got als an error. I tried so many things but always the same. For calling the function I just call the name of the data script that I named visual_gauss like visual_gauss(1,2,0) for example in the command window.

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

답변 (2개)

Jan
Jan 2015년 5월 17일
편집: Jan 2015년 5월 17일

0 개 추천

You do not need the elementwise multiplication .* but the matrix multiplication *
Z = 1/((2*pi)^(n/2) * sqrt(det(P))) * exp((-1/2) * x' * Pinv * x);
Note: pi is not 3.41 ! Even 3.14 would be a too rough simplification.

댓글 수: 4

corny
corny 2015년 5월 17일
sorry for pi I was just trying a number because I got an error at first with pi but now it is working. I tried with multiplication but always the same Error like this: Error using * Inner matrix dimensions must agree.
Error in visual_gauss1 (line 16) Z= 1/((2*pi)^(n/2)*sqrt(det(P)))*exp((-1/2)*x' * Pinv * x) ;
Jan
Jan 2015년 5월 17일
편집: Jan 2015년 5월 17일
@corny: Then this is a job for the debugger. Set a breakpoint in this line and check the sizes of x and Pinv. Then remember the definition of the matrix product and see, if there is a mistake. I assume "c" is a scalar. Then Pinv is a 2x2 matrix. But x has 41 elements. Therefore a multiplication must fail.
It is confusing, that the posted code contains a lower case x, while the error message contains an uppercase X. Please case for posting only the code, which is actually used.
corny
corny 2015년 5월 17일
It's true, Pinv and x haven't the same dimension. But as I know we should use the point here to make the operation work. That's why I used .* in the place of * so that the multiplication works element by element. For x, I tried to change it by using X what is more logic ist or?
Jan
Jan 2015년 5월 17일
@corny: According to the small snippet of the original question x is a vector. We cannot see the complete question and do not have a chance to guess, what you are wanting to achieve. When you post one code and the error message produced by other code, the confusion is perfect.

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

Walter Roberson
Walter Roberson 2015년 5월 17일

0 개 추천

Your X is 1 x 41. Your Pinv is 2 x 2. What are you expecting Pinv * x to mean?
I suspect this is what you want:
x= linspace(-1.6*max(s1,s2), 1.6*max(s1,s2),41);
y= linspace(-1.6*max(s1,s2), 1.6*max(s1,s2),41);
[X,Y]= meshgrid(x,y);
Xv = X(:);
Yv = Y(:);
XY = [Xv, Yv];
Now XY is (41*41) by 2 and you can do
XY * Pinv * XY'

카테고리

질문:

2015년 5월 17일

댓글:

Jan
2015년 5월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by