issue with distance calculation ?
3 次查看(过去 30 天)
显示 更早的评论
I have an issue with distance calculation, and I used the code bellow to calculate it:
k=2;
D=data;
[N m]= size (data);
R_I = randsample(N,k);
Cnt = D(R_I,:);
while(true)
for l=1:N
for i=1:k
dist(:,i) = sqrt( (D - repmat(Cnt(i,:),N,1))*((Sigma)^-1) * (D -repmat(Cnt(i,:),N,1))');
end
end
dist=sqrt(dist);
I'm receiving an error which is:
Subscripted assignment dimension mismatch.
1 个评论
Jan
2017-6-26
编辑:Jan
2017-6-26
Please post te complete error message. Do not let the readers guess, where the problem occurres. The shown procedure is not an Euclidean distance: There is at least a SQRT too much. Since Matlab R2016b the REPMAT can be omitted.
... / Sigma
is nicer and much faster than
... *((Sigma)^-1)
The contents of the loops do not depend on the loop counter "l", therefore the same calculation is performed N times.
回答(3 个)
Jan
2017-6-26
编辑:Jan
2017-6-26
N is the length of the 2nd dimension of data:
[m N]= size (data);
Then you coose two elements from 1:N:
R_I = randsample(N,k);
But then you use the indices in the 1st dimension:
Cnt = D(R_I,:);
Either
R_I = randsample(m, k);
or
Cnt = D(:, R_I);
Please explain, what you want to achieve. The code can be simplified substantially.
9 个评论
Image Analyst
2017-6-27
编辑:Image Analyst
2017-6-27
This is confusing to me. Because D and Cnt are matrices and you're subtracting and square rooting them, you're basically doing something with their values, not distances, like I answered in this other question. So if the values of two points in the matrices were 3000 and 4000, and they were in the same row but in column 3 and column 5, you'd get a "distance" of 1000 (difference in values), not a "distance" of 2 (difference in lateral distance in the row-column plane). So I don't know if you want the "distance" in "value space" or "x-y location space". Which way do you want it?
0 个评论
John BG
2017-6-29
编辑:John BG
2017-6-29
Hi Shawin
correct if wrong, but it seems you are aiming at calculating the dissimilarity between observations and expectations.
From
you want to calculate the distance
d(x,y)=((x-y)'*S^-1*(x-y))^.5
where x and y are vectors that have to be same length. Generating shorter data than the .csv file you have kindly attached
clear all; close all
N=10;a=-10;b=10;
r1x=(b-a)*rand(N,1)+a; r1y=(b-a)*rand(N,1)+a;
r2x=(b-a)*rand(N,1)+a; r2y=(b-a)*rand(N,1)+a;
x1=[r1x r1y];x2=[r2x r2y];
x1 =
-4.9639 8.1262
-4.1912 7.5931
2.3418 6.3552
-4.6944 -4.7854
6.4875 1.8871
9.6533 -9.5497
4.6050 -1.4948
-3.1225 -3.7456
1.6814 -6.7703
-7.8446 -6.4247
x2 =
-1.5423 0.6173
-8.1154 3.0889
1.9705 -1.8476
-0.5815 6.3996
3.9190 4.3672
3.9978 9.3730
2.7706 0.6267
-9.3279 -3.4971
-8.6239 -7.8874
-3.6080 2.2192
S=cov(x1,x2)
S =
35.8692 6.4390
6.4390 27.6440
.
rather than
(S^-1*(x1-x2)')*(x1-x2)
=
7.4389 -5.7005
-2.8055 27.1493
try mahdist and Covariance functions typed following these lines
d1=mahdist(x1,x2)
=
1.2241
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
%%%%
the support functions, from from http://people.revoledu.com/kardi/tutorial/Similarity/MahalanobisDistance.html
function d=mahdist(A,B)
[n1 k1]=size(A);
[n2 k2]=size(B);
n=n1+n2;
if (k1~=k2)
disp('number of columns of A and B msut be equal')
else
xDiff=mean(A)-mean(B); % mean diff row vector
cA=Covariance(A)
cB=Covariance(B)
pC=n1/n/n*cA+n2/n*cB % pooled covariance matrix
d=(xDiff*inv(pC)*xDiff').^.5;
end
end
function C=Covariance(X)
[n,k]=size(X)
Xc=X-repmat(mean(X),n,1)
C=Xc'*Xc/n
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!