issue with distance calculation ?

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 个评论

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
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 个评论

I'm calculating the distance according to the equation that I provide with the code, sorry about N it is the typo error, the issue is with looping and producing the distance matrix, my data is 100x2 matrix, the distance matrix should be 100x2.
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)
If the data matrix is 100x2, it means you have 100 points.
The distance matrix of 100 points is 100x100, not 100x2
John BG
@Jan I tested The formula that you suggested:
Dist = sqrt(sum((D - Point) .^ 2, 2) / Sigma);
but I'm receiving error which is :
Matrix dimensions must agree.
and the code that is in the link which you sent has the same issue:
X= rand(1000, 3);
Pick = 17;
Point = X(Pick, :);
Dist = sqrt(sum((X - Point) .^ 2, 2)); % >= R2016b
@jan, The distance is calculated between each column of the data and each center, and what is the repmat() is doing is repeat the cent 100 times so that the distance will be calculated between each point and the center , and same for the second center
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.
@shawin: Wich Matlab version are you using?
shawin
shawin 2017-6-28
编辑:shawin 2017-6-28
I'm using matlab 2015b
@Image analysis: I attached the data
Jan
Jan 2017-6-29
编辑:Jan 2017-6-29
@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.

请先登录,再进行评论。

Image Analyst
Image Analyst 2017-6-27
编辑:Image Analyst 2017-6-27

0 个投票

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 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
%%%%
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

类别

帮助中心File Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

提问:

2017-6-26

编辑:

2017-6-29

Community Treasure Hunt

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

Start Hunting!

Translated by