issue with distance calculation ?

1 view (last 30 days)
shawin
shawin on 26 Jun 2017
Edited: John BG on 29 Jun 2017
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 Comment
Jan
Jan on 26 Jun 2017
Edited: Jan on 26 Jun 2017
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.

Sign in to comment.

Answers (3)

Jan
Jan on 26 Jun 2017
Edited: Jan on 26 Jun 2017
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 Comments
shawin
shawin on 28 Jun 2017
Edited: shawin on 28 Jun 2017
I'm using matlab 2015b
@Image analysis: I attached the data
Jan
Jan on 29 Jun 2017
Edited: Jan on 29 Jun 2017
@shawin: Did you recognize the hint ">= R2016b" and used the bsxfun solution?
Now we can run your code and get the same error. But you still did not explain, what you want to achieve. The failing code does not clarify this uniquely.

Sign in to comment.


Image Analyst
Image Analyst on 27 Jun 2017
Edited: Image Analyst on 27 Jun 2017
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?

John BG
John BG on 29 Jun 2017
Edited: John BG on 29 Jun 2017
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
%%%%
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

Tags

Community Treasure Hunt

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

Start Hunting!