Calculating the distance matrix from coordiante matrix

51 visualizaciones (últimos 30 días)
Nour BS
Nour BS el 1 de Feb. de 2016
Comentada: Nour BS el 1 de Feb. de 2016
Hello,
How can I calculate the distance matrix? for example I have the coordinate matrix: A=[3 5 4 5; 1 2 6 3] and I want to find the distance between each pair of points.
Thanks in advance for any help!

Respuesta aceptada

Kelly Kearney
Kelly Kearney el 1 de Feb. de 2016
If you have the Stats Toolbox, pdist, as already mentioned:
A=[3 5 4 5; 1 2 6 3]
squareform(pdist(A'))
ans =
0 2.2361 5.099 2.8284
2.2361 0 4.1231 1
5.099 4.1231 0 3.1623
2.8284 1 3.1623 0
If you don't have the Stats Toolbox, you can calculate the distance manually:
dx = bsxfun(@minus, A(1,:), A(1,:)');
dy = bsxfun(@minus, A(2,:), A(2,:)');
sqrt(dx.^2 + dy.^2)
ans =
0 2.2361 5.099 2.8284
2.2361 0 4.1231 1
5.099 4.1231 0 3.1623
2.8284 1 3.1623 0

Más respuestas (1)

Walter Roberson
Walter Roberson el 1 de Feb. de 2016
pdist() if you have the stats toolbox
  3 comentarios
Walter Roberson
Walter Roberson el 1 de Feb. de 2016
What is "matrix distance" of points?
pdist allows you to pass any of several different distance function names as a string in the second parameter, or you can pass a custom distance function by function handle.
Nour BS
Nour BS el 1 de Feb. de 2016
d=Boxdist(A) ==> d =
0 2 5 2
2 0 4 1
5 4 0 3
2 1 3 0
But this result is not exact... I must find: d =
0 2.2361 5.099 2.8284
2.2361 0 4.1231 1
5.099 4.1231 0 3.1623
2.8284 1 3.1623 0

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by