MATLAB Answers

could anyone help me to calculate the euclidean distance for the matrix.

15 ビュー (過去 30 日間)
jaah navi
jaah navi 2019 年 9 月 11 日
回答済み: Bruno Luong 2019 年 9 月 25 日
A= [1.8667 0.1553;
-0.0844 2.4322;
-0.3485 1.4434;
2.3628 0.6821]
I want to calculate the euclidean distance of first row with respect to second,third and fourth row.
Similarly i want to calculate the euclidean distance of second row with respect to first,third and fourth row and so on.
could anyone please help me onthis.

  0 件のコメント

サインイン to comment.

採用された回答

Stephen Cobeldick
Stephen Cobeldick 2019 年 9 月 11 日
編集済み: Stephen Cobeldick 2019 年 9 月 11 日
>> B = sqrt(sum(bsxfun(@minus,permute(A,[1,3,2]),permute(A,[3,1,2])).^2,3))
B =
0.00000 2.99851 2.56248 0.72363
2.99851 0.00000 1.02346 3.00859
2.56248 1.02346 0.00000 2.81615
0.72363 3.00859 2.81615 0.00000
Or use pdist (requires the Statistics Toolbox):

  1 件のコメント

jaah navi
jaah navi 2019 年 9 月 11 日
i used pdist to calculate the euclidean distance with respect to the following code;
code:
PPP=[1.8667 0.1553;
-0.0844 2.4322;
-0.3485 1.4434;
2.3628 0.6821]
D = pdist(PPP)
f=sum(D)/numel(D)
indices = find(abs(D)<f)
D(indices) = []
when i execute the above code i can get the result as
D = [2.9985 2.5625 3.0086 2.8162]
but i want to display which two rows gives the above result.the expected output is
D=[(1,2) (1,3) (2,4) (3,4)]
Could you please help me on it.

サインイン to comment.

その他の回答 (5 件)

Christine Tobler
Christine Tobler 2019 年 9 月 11 日
For MATLAB R2017b or later, you can use the vecnorm function for a simpler construction than the one involving sqrt, sum, and .^2:
vecnorm(permute(A,[1,3,2]) - permute(A,[3,1,2]), 2, 3)
This will give the exact same result as constructing two for-loops and computing B(i, j) = norm(x(i, :) - x(j, :)) individually for each combination.

  0 件のコメント

サインイン to comment.


Fabio Freschi
Fabio Freschi 2019 年 9 月 11 日
編集済み: Fabio Freschi 2019 年 9 月 11 日
I have found that this is usually the fastest way, since the square of the binomial is unrolled
D = sqrt(abs(bsxfun(@plus,sum(A.*A,2),sum(A.*A,2).')-2*A*A'));
If you have two sets of points A and B
D = sqrt(abs(bsxfun(@plus,sum(A.*A,2),sum(B.*B,2).')-2*A*B'));

  3 件のコメント

Fabio Freschi
Fabio Freschi 2019 年 9 月 11 日
Check
>> N = 10000;
>> A = rand(N,2);
>> tic; D = sqrt(abs(bsxfun(@plus,sum(A.*A,2),sum(A.*A,2).')-2*A*A')); toc
Elapsed time is 1.245305 seconds.
>> tic; B = sqrt(sum(bsxfun(@minus,permute(A,[1,3,2]),permute(A,[3,1,2])).^2,3)); toc
Elapsed time is 1.589360 seconds.
Bruno Luong
Bruno Luong 2019 年 9 月 11 日
The decompose method might have worst roundoff numerical issue:
A=[1e9 0;
1e9+1 0]
D = sqrt(abs(bsxfun(@plus,sum(A.*A,2),sum(A.*A,2).')-2*A*A'))
B = sqrt(sum(bsxfun(@minus,permute(A,[1,3,2]),permute(A,[3,1,2])).^2,3))
B is correct D is not.
It could even return complex numbers.
Fabio Freschi
Fabio Freschi 2019 年 9 月 11 日
Good point @Bruno Luong
I used this function in a "safe" environment and never met this condition. In any case, thanks for pointing out

サインイン to comment.


Bruno Luong
Bruno Luong 2019 年 9 月 11 日
I would put as comment of Christine's vecnorm soluton, but somehow Answers rejects it.
The vecnorm is slower than standard solution (Stephen's) in case of p = 2.
N = 10000;
A = rand(N,2);
tic
B = sqrt(sum((permute(A,[1,3,2])-permute(A,[3,1,2])).^2,3));
toc % Elapsed time is 1.249531 seconds.
tic
C = vecnorm(permute(A,[1,3,2]) - permute(A,[3,1,2]), 2, 3);
toc % Elapsed time is 4.590562 seconds.

  0 件のコメント

サインイン to comment.


Christine Tobler
Christine Tobler 2019 年 9 月 24 日
編集済み: Christine Tobler 2019 年 9 月 24 日
Hi Bruno, I have the same problem where I can't comment on your answer, so adding another answer here.
That's a good point - vecnorm returns the exact same value as vecnorm, however it's not been optimized for performance as sum. Still, with all the additional overhead in the computation using sum, .^2 and sqrt, it would make sense for vecnorm to be faster here. I've made an enhancement request for this.

  0 件のコメント

サインイン to comment.


Bruno Luong
Bruno Luong 2019 年 9 月 25 日
Hi Christtine, very good initiative. I think at least 90% of use-case would be p=2.

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。


Translated by