Standard builtin function for euclidean distance matrix?

2 ビュー (過去 30 日間)
Robert
Robert 2015 年 6 月 10 日
コメント済み: Image Analyst 2015 年 6 月 10 日
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 Center および File ExchangeStatistics 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