issue with distance calculation ?
Mostrar comentarios más antiguos
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 comentario
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.
Respuestas (3)
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 comentarios
shawin
el 26 de Jun. de 2017
Does this thread concern the same problem: https://www.mathworks.com/matlabcentral/answers/346313-how-can-i-find-distances-to-the-chosen-point-from-all-the-other-points-euclidean-distance ?
Please excplain, what you want to do. Why do you select 2 points? Which distance should be calculated? I assume you get something like this fianlly, without loops:
Dist = sqrt(sum((D - Point) .^ 2, 2) / Sigma)
John BG
el 27 de Jun. de 2017
If the data matrix is 100x2, it means you have 100 points.
The distance matrix of 100 points is 100x100, not 100x2
John BG
shawin
el 27 de Jun. de 2017
Image Analyst
el 27 de Jun. de 2017
shawn, please attach "data" so we can try your code. A txt file or mat file would be fine, along with the code used to get "data" in from the file to MATLAB.
Jan
el 27 de Jun. de 2017
@shawin: Wich Matlab version are you using?
Image Analyst
el 27 de Jun. de 2017
Editada: Image Analyst
el 27 de Jun. de 2017
0 votos
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?
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
Categorías
Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!