problem with Matrix Multiplication

2 views (last 30 days)
shawin
shawin on 13 Jan 2017
Answered: Are Mjaavatten on 13 Jan 2017
I have a matrix D which is 10x2, and a center Cent is 2x2 and a w diagonal matrix which is 2x2 , i have an equation:
Result = 1/W^2*(sqrt(w*(X-Y)*SIGMA^(-1)*w*(X-Y)'));
the code which i used is:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
D= [0.1 0.2 ; 0.3 0.4
0.03 0.7; 0.1 0.7
0.08 0.06; 0.04 0.9
0.11 0.13; 0.12 0.32
0.21 0.71; 1.01 0.33
];
Sigma= cov(D);
Cnt=[-1.982,1.278;0.431,-0.278]
k=2;
w=[0.6 0; 0 0.4];
Wp= 0.6^2 +0.4^2;
N=length(D);
dist=zeros(k,1);
for l=1:N
for i=1:k
dist(i) = 1/Wp*(sqrt( w*(D(l,:)-Cnt(i,:)) * inv(Sigma) * W*(D(l,:)-Cnt(i,:))'));
end
end
iam receiving error : Error using * Inner matrix dimensions must agree.
any help please

Answers (1)

Are Mjaavatten
Are Mjaavatten on 13 Jan 2017
Note that in Matlab, vectors are matrices of column or row shape and. A 2 by 2 matrix can only be multiplied by a column vector, not by a row vector.
x = (D(l,:)-Cnt(i,:)) is a 1 by 2 matrix ( row vector)
w is a 2 by 2 matrix, so w*x will result in an error
Maybe what you want to do is to replace the content of the innermost loop by:
x = (D(l,:)-Cnt(i,:));
dist(i) = 1/Wp*(sqrt( x*W * inv(Sigma) * W'*x'));

Community Treasure Hunt

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

Start Hunting!