Error using * Matrix dimensions must agree. please help.
조회 수: 1 (최근 30일)
이전 댓글 표시
N_BF=10;
N_s=700;
A1=zeros(N_BF,N_BF);
Wu=zeros(N_BF,N_BF);
Ww=zeros(N_BF,N_BF);
b1=zeros(N_BF,1);
% compute interval
t2=cputime;
for i=1:N_s
x1=-1300+rand; x2=0+rand; x3=0+rand;
f=[x2/x3; -x1*x2/x3; x1];
BF=[ x1^2; x1*x2; x1*x3; x2^2 ;x2*x3 ; x3^2 ;
x1/x3; x1*x2/x3 ; x2^2/x3 ; x2^4/x3^2];
DBF= [ 2*x1 0 0
x2 x1 0
x3 0 x1
0 2*x2 0
0 x3 x2
0 0 2*x3
1/x3 0 -x1/x3^2
x2/x2 x1/x3 -x1*x2/x3^2
0 2*x2/x3 -x2^2/x3^2
0 4*x2^3 -2*x2^4/x3^3];
Aa=[x1;x2;x3]'*[x1;x2;x3];
A1=A1+BF*f'*DBF';
b1=b1+BF*Aa;
g1=[0;1;0]';
g2=[0;-1;0]';
for j= 1:N_BF
Wu(:,:,j)=Wu(:,:,j)+BF*DBF(j,:)*g2*g2'*DBF';
Ww(:,:,j)=Ww(:,:,j)+BF*DBF(j,:)*g1*g1'*DBF';
end
end
getting the error at Wu(:,:,j)=Wu(:,:,j)+BF*DBF(j,:)*g2*g2'*DBF';
댓글 수: 0
채택된 답변
Abhiram Bhanuprakash
2014년 10월 28일
편집: Abhiram Bhanuprakash
2014년 10월 28일
Hi JJ Kena,
I think the error is because one row of DBF is 1X3, and g2 is also 1X3. So, when it tries to multiply both of them, you get the dimension mismatch error.
Paranthesizing the g2*g2' term would resolve the issue.
Thus, change the two lines of code to:
Wu(:,:,j)=Wu(:,:,j)+BF*DBF(j,:)*(g2*g2')*DBF';
Ww(:,:,j)=Ww(:,:,j)+BF*DBF(j,:)*(g1*g1')*DBF';
and you would be able to run it.
Also, if you observe, MATLAB shows an orange symbol in the Editor window for the two lines, and if you hover over it, you can see a message which says 'Paranthesize the multiplication to ensure the result is Hermitian'.
Hope this resolves your issue. MATLAB rocks!
Cheers!
추가 답변 (2개)
yonatan gerufi
2014년 10월 28일
well, your line:
Wu(:,:,j)=Wu(:,:,j)+BF*DBF(j,:)*g2*g2'*DBF';
is totally messed up with dimension.
think what dimentions each parameter should be, and see the difference with debug mode.
saying all that, pay attention that if you want element-by-element multiplication you should use "."
e.g. A.*B
good luck.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!