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!