Error using * Matrix dimensions must agree. please help.

1 Ansicht (letzte 30 Tage)
jj kena
jj kena am 28 Okt. 2014
Bearbeitet: jj kena am 29 Okt. 2014
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';

Akzeptierte Antwort

Abhiram Bhanuprakash
Abhiram Bhanuprakash am 28 Okt. 2014
Bearbeitet: Abhiram Bhanuprakash am 28 Okt. 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!
  1 Kommentar
jj kena
jj kena am 28 Okt. 2014
Bearbeitet: jj kena am 29 Okt. 2014
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);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

yonatan gerufi
yonatan gerufi am 28 Okt. 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.

jj kena
jj kena am 29 Okt. 2014
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.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by