[Numpy-discussion] finding minimum distance using arrays
Wed Apr 23 12:15:54 CDT 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...
Christopher Barker, Ph.D.
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
More information about the Numpy-discussion