필터 지우기
필터 지우기

Calculating the distance matrix from coordiante matrix

조회 수: 20 (최근 30일)
Nour BS
Nour BS 2016년 2월 1일
댓글: Nour BS 2016년 2월 1일
Hello,
How can I calculate the distance matrix? for example I have the coordinate matrix: A=[3 5 4 5; 1 2 6 3] and I want to find the distance between each pair of points.
Thanks in advance for any help!

채택된 답변

Kelly Kearney
Kelly Kearney 2016년 2월 1일
If you have the Stats Toolbox, pdist, as already mentioned:
A=[3 5 4 5; 1 2 6 3]
squareform(pdist(A'))
ans =
0 2.2361 5.099 2.8284
2.2361 0 4.1231 1
5.099 4.1231 0 3.1623
2.8284 1 3.1623 0
If you don't have the Stats Toolbox, you can calculate the distance manually:
dx = bsxfun(@minus, A(1,:), A(1,:)');
dy = bsxfun(@minus, A(2,:), A(2,:)');
sqrt(dx.^2 + dy.^2)
ans =
0 2.2361 5.099 2.8284
2.2361 0 4.1231 1
5.099 4.1231 0 3.1623
2.8284 1 3.1623 0

추가 답변 (1개)

Walter Roberson
Walter Roberson 2016년 2월 1일
pdist() if you have the stats toolbox
  댓글 수: 3
Walter Roberson
Walter Roberson 2016년 2월 1일
What is "matrix distance" of points?
pdist allows you to pass any of several different distance function names as a string in the second parameter, or you can pass a custom distance function by function handle.
Nour BS
Nour BS 2016년 2월 1일
d=Boxdist(A) ==> d =
0 2 5 2
2 0 4 1
5 4 0 3
2 1 3 0
But this result is not exact... I must find: d =
0 2.2361 5.099 2.8284
2.2361 0 4.1231 1
5.099 4.1231 0 3.1623
2.8284 1 3.1623 0

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

Community Treasure Hunt

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

Start Hunting!

Translated by