0

I think this has been asked before, but I'm trying to implement the following: I've got a list of tuples containing the 2D coordinates of the N particles that I have. I have defined a numpy.zeros((N,N)) array to store the distance between them. How could I do this the fastest?

Thanks in advance for any help! :)

Edited to add: I've already written a function to measure the distance between two tuples, and was wondering how to iterate it!

My distance measuring function:

def calc_distance(p1, p2):
    distance = numpy.linalg.norm(p1 - p2)
    return distance
0

1 Answer 1

2

Distance matrix is what you are looking for:

coords = [(0,0), (1,1), (3,2)]

from scipy.spatial import distance_matrix

distance_matrix(coords, coords)

Output:

array([[0.        , 1.41421356, 3.60555128],
       [1.41421356, 0.        , 2.23606798],
       [3.60555128, 2.23606798, 0.        ]])
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, thanks a million, that's exactly what I was looking for! EDIT: Is there a way to do it with numpy?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.