[Numpy-discussion] finding minimum distance using arrays
Christopher Barker
Chris.Barker at noaa.gov
Wed Apr 23 13:15:54 EDT 2008
Nadav Horesh wrote:
> def distance(v1,v2):
> return sqrt(((v2-v1)**2).sum())
note that Roberts version didn't compute the sqrt, as to find the
minimum distance, you can just used the squared value.
If you do want the actual distance, then you might want to use
numpy.hypot, something like (untested):
import numpy as np
def distance(v1, v2):
diff = v1-v2
return np.hypot(diff[:,0], diff[:,1])
It should be faster -- less data copying, one loop.
By the way, it would be nice to have a version that would take a NX2
array, so you could write: np.hypot(v1-v2)
or even np.EuclidDistance(v1, v2)
But maybe it's not worth bloating numpy for...
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the NumPy-Discussion
mailing list