Calculating the distance between all points in an area with each other

I have an area with 144 points. What I want to achieve is to measure the distance of a point with all the others and save it in an array. I want to do this for all points. If possible, I would like to store this data in a way that does not repeat it. And I should be able to make such requests: all distances between all points without repetition, the sum of all distances for point No. 56, etc.

I have a 3 * 144 array with two columns storing the coordinates of the points.

+7
source share
3 answers

Possible solution (I do not quite understand what you mean by repetition):

X are your points with coordinates x = X(:,1), y = X(:,2) dist = sqrt(bsxfun(@minus,X(:,1),X(:,1)').^2 + bsxfun(@minus,X(:,2),X(:,2)').^2) 

So

 dist(i,j) is the euclidean distance between i and j 

Of course, the matrix is โ€‹โ€‹symmetric. You can easily reduce complexity.

+6
source

Let's say your array is A , where each column stores the coordinates of one point. To get combinations of all pairs of points (no repetitions), use nchoosek :

 pairs = nchoosek(1:size(A, 2), 2) 

Then calculate the Euclidean distance , for example:

 dist = sqrt(sum((A(:, pairs(:, 1)) - A(:, pairs(:, 2))) .^ 2, 1)) 

If you have the Statistics Tool installed, you can use pdist(A) instead of the same effect.

+2
source

If you have a statistics toolbar, and if you have all the data in an X array, then

 D = pdist(X) 

gives all pairwise distances between all points in X.

+1
source

All Articles