Standard builtin function for euclidean distance matrix?

2 visualizaciones (últimos 30 días)
Robert
Robert el 10 de Jun. de 2015
Comentada: Image Analyst el 10 de Jun. de 2015
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.

Respuestas (2)

Konstantinos Sofos
Konstantinos Sofos el 10 de Jun. de 2015
Editada: Konstantinos Sofos el 10 de Jun. de 2015
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 el 10 de Jun. de 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 comentarios
Image Analyst
Image Analyst el 10 de Jun. de 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
Image Analyst el 10 de Jun. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Statistics and Machine Learning Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by