Error using * Matrix dimensions must agree. please help.
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Abhiram Bhanuprakash
on 28 Oct 2014
Edited: Abhiram Bhanuprakash
on 28 Oct 2014
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!
More Answers (2)
yonatan gerufi
on 28 Oct 2014
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 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!