Calculating the distance matrix from coordiante matrix
조회 수: 20 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
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
2016년 2월 1일
pdist() if you have the stats toolbox
댓글 수: 3
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!