Distance Between a Set of Points

조회 수: 27 (최근 30일)
Suyapa Gonzalez
Suyapa Gonzalez 2015년 6월 18일
댓글: Walter Roberson 2018년 11월 17일
I have a list of X and Y values and I need to get the distance between each point. So basically, the distance between point 1 to the other 14 points, point 2 to the other 14 points, point 3 to the other 14 points, for example. How do I do this without manually plotting in every pair of coordinates???

답변 (3개)

Joseph Cheng
Joseph Cheng 2015년 6월 18일
편집: Joseph Cheng 2015년 6월 18일
you can use the function nchoosek to generate the pairs:
xy= randi(10,15,2)
pairs = nchoosek(1:5,2)
p1 = xy(pairs(:,1),:);
p2 = xy(pairs(:,2),:);
distance = sqrt(sum((p1-p2).^2,2));
  댓글 수: 2
Suyapa Gonzalez
Suyapa Gonzalez 2015년 6월 18일
Where do I plug in my data points? I have 15 different pair of coordinates.
Joseph Cheng
Joseph Cheng 2015년 6월 18일
it would be the xy points, since i do not have your data i created my own list of X and Y values.

댓글을 달려면 로그인하십시오.


Jacob Strang
Jacob Strang 2018년 11월 17일
I believe that pdist does this automatically if you provide more than 2 points, as seen in the first example on the linked page:
% Compute the Euclidean distance between pairs of observations, and convert the distance vector to a matrix using squareform.
% Create a matrix with three observations and two variables.
rng('default') % For reproducibility
X = rand(3,2);
% Compute the Euclidean distance.
D = pdist(X)
% D = 1×3
% 0.2954 1.0670 0.9448
% The pairwise distances are arranged in the order (2,1), (3,1), (3,2). You can easily locate the distance between observations i and j by using squareform.
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 11월 17일
You can use squareform() to convert the pdist() triangular matrix result to a symmetric square matrix.

댓글을 달려면 로그인하십시오.


Chad Greene
Chad Greene 2015년 6월 18일
Or there's John D'Errico's ipdm function.

Community Treasure Hunt

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

Start Hunting!

Translated by