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

조회 수: 1 (최근 30일)
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.

채택된 답변

Stephen23
Stephen23 2019년 9월 11일
편집: Stephen23 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.

댓글을 달려면 로그인하십시오.

추가 답변 (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.

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

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
Bruno Luong
Bruno Luong 2019년 9월 11일
편집: 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

댓글을 달려면 로그인하십시오.


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.

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.

카테고리

Help CenterFile Exchange에서 Geometric Geodesy에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by