Error using * Matrix dimensions must agree. please help.
2 次查看(过去 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!