Standard builtin function for euclidean distance matrix?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponses (2)
Konstantinos Sofos
le 10 Juin 2015
Modifié(e) : Konstantinos Sofos
le 10 Juin 2015
% 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,
0 commentaires
Image Analyst
le 10 Juin 2015
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 commentaires
Image Analyst
le 10 Juin 2015
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
le 10 Juin 2015
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.
Voir également
Catégories
En savoir plus sur Statistics and Machine Learning Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!