Standard builtin function for euclidean distance matrix?

2 次查看(过去 30 天)
My apologies for the simplicity of this question, but is there a simple builtin function for calculating the Euclidean distances between two sets of coordinates? nearestNeighbor provides the value for the nearest neighbor, but I would like the full matrix of distances.

回答(2 个)

Konstantinos Sofos
Konstantinos Sofos 2015-6-10
编辑:Konstantinos Sofos 2015-6-10
what about pdist?
% Compute the ordinary Euclidean distance.
X = [1,2,3;4,5,6;7,8,9];
D = pdist(X,'euclidean'); % euclidean distance
D_Matrix = squareform(D)
D_Matrix =
0 5.1962 10.3923
5.1962 0 5.1962
10.3923 5.1962 0
As you can see, the distance between e.g. element (1,1) and (1,2) is 5.1962. To check,
sqrt((4-1)^2+(5-2)^2+(6-3)^2)=sqrt(27)=5.1962
Regards,

Image Analyst
Image Analyst 2015-6-10
Yes. Try hypot() for getting distance between two points. For doing multiple pairs, if you have the Statistics and Machine Learning Toolbox you can use pdist() which gets all distances between all possible permutations of point pairings (point 1 to 2, 1 to 3, 1 to 4, 2 to 3, 2 to 4, etc.)
  2 个评论
Image Analyst
Image Analyst 2015-6-10
Robert's "Answer" moved here:
Hi Konstantinos and Image, unfortunately, pdist requires the Stats package, which I do not have. Looks like with out it, its down to doing it the hard way.
repmat and hybot with some organizing will get the job done. Its such a simple function, I assumed there would be a builtin function in the standard package for the task.
Thanks for the input.
Image Analyst
Image Analyst 2015-6-10
Yes, hypot(), like I suggested is what I usually use because I usually go between a pair of points. I'm not sure why repmat() would be needed though.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by