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';

采纳的回答

Abhiram Bhanuprakash
编辑: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!
  1 个评论
jj kena
jj kena 2014-10-28
编辑:jj kena 2014-10-29
Abhiram Bhanuprakash, thanks alot, did that n saw another problem too, its at
Wu=zeros(N_BF,N_BF); Ww=zeros(N_BF,N_BF); these two shlould be, Wu=zeros(N_BF,N_BF,N_BF); Ww=zeros(N_BF,N_BF,N_BF);

请先登录,再进行评论。

更多回答(2 个)

yonatan gerufi
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.

jj kena
jj kena 2014-10-29
yonatan gerufi ,from BF*DBF(j,:)*g2*g2'*DBF', BF is 10x1, BDF(j,:) is 1x3, g2 is 3x1, g2' is 1x3, and DBF' is 3x10, if i think you can multiply those matrices without a problem to give u a 10x10 matrice.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by