Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating distance of all the points in a region with each other

I have a region with about 144 points. What i want to achieve is to measure the distance of a point with all others and store it in an array. I want to do this for all the points. If possible i would like to store this data in such a manner that there in no repetition. And i should be able to make queries like- All the distances between all the points without repetition, sum of all the distances for point no56 etc.

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

like image 385
Vikram Avatar asked Jan 23 '13 15:01

Vikram


1 Answers

A possible solution (I am not really clear with what you mean by no repetition, though):

 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 the complexity involved.

like image 106
Acorbe Avatar answered Sep 26 '22 15:09

Acorbe