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 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