2

I am working with a list of points [(1,2),(3,4),(5,6),(7,8)]. I want to find the euclidean distance from each point to every other point in the list.

I then need a new list created to represent each point in the original list, and in the new list I will add the distances relating to that point only.

So far I have:

for i in mat_ary1:
    points_dist_i = []
    for j in i:
        row = []
        x2 = [u[0] for u in i]
        y2 = [u[1] for u in i]
        # Calculate the distance from point j to all others
        for a in x2:
            dist_x_1 = pow((a - j[0]),2)     
        for b in y2:        
            dist_y_1 = pow((b - j[1]),2) 
            dist_xy_1 = float('{0:.2f}'.format((math.sqrt(dist_x_1 + dist_y_1))))

            for item in j: 
                if item not in row:
                    row.append(dist_xy_1)
                else:
                    continue
                points_dist_i.append(row) 

Each i in mat_ary1 represents a list of points. With the loops I am using I appear to repeating the same calculations.

My input seems to be duplicating the rows:

[[6.32, 6.32], [6.32, 6.32], [0.0, 0.0], [0.0, 0.0]]
[[11.4, 11.4], [11.4, 11.4], [0.0, 0.0], [0.0, 0.0]]
[[16.49, 16.49], [16.49, 16.49], [0.0, 0.0], [0.0, 0.0]]
[[14.32, 14.32], [14.32, 14.32], [0.0, 0.0], [0.0, 0.0]]
[[13.0, 13.0], [13.0, 13.0], [0.0, 0.0], [0.0, 0.0]]
[[11.66, 11.66], [11.66, 11.66], [0.0, 0.0], [0.0, 0.0]]
4
  • let me make sure i understand you. Say we have two points, (1, 2) and (3, 4). for those two points you want to find the distance between 1 and 3. And 2 and 4? Commented Aug 29, 2016 at 20:03
  • What exactly is the output you want? Commented Aug 29, 2016 at 20:07
  • @Mr.goosberry - yes that's what I want but my lists sizes always vary Commented Aug 29, 2016 at 20:10
  • @juanpa.arrivillaga - say if I have a list of four points, I want four lists that give the distance to every point in that list. Commented Aug 29, 2016 at 20:11

2 Answers 2

3

You can use the following nested list comprehension

>>> import math
>>> [[math.hypot(point[0]-x, point[1]-y) for x,y in points] for point in points]
[[0.0, 2.8284271247461903, 5.656854249492381, 8.48528137423857],
 [2.8284271247461903, 0.0, 2.8284271247461903, 5.656854249492381], 
 [5.656854249492381, 2.8284271247461903, 0.0, 2.8284271247461903], 
 [8.48528137423857, 5.656854249492381, 2.8284271247461903, 0.0]]

This essentially makes a matrix with the distance from one point to any other point, where the row and column indexes are the "from" and "to" points, in which case the matrix will also be symmetric about the diagonal, and the diagonal will be all zeros.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok I'll give this a go and see how I get on.
Thanks man, that worked a treat. Saved me from all those for loops!
2

Scikit-learn has a function for this exact problem, and it will probably be the fastest implementation if your array is large.

>>>>from sklearn.metrics.pairwise import pairwise_distances
>>>>pairwise_distances(mat_ary1)
array([[ 0.        ,  2.82842712,  5.65685425,  8.48528137],
   [ 2.82842712,  0.        ,  2.82842712,  5.65685425],
   [ 5.65685425,  2.82842712,  0.        ,  2.82842712],
   [ 8.48528137,  5.65685425,  2.82842712,  0.        ]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.