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
Joker
  • 81
  • 5

1 Answers1

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.        ]])
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • Hey, thanks a million, that's exactly what I was looking for! EDIT: Is there a way to do it with numpy? – Joker Dec 09 '19 at 19:20