[Numpy-discussion] finding minimum distance using arrays
Nadav Horesh
nadavh@visionsense....
Wed Apr 23 01:14:17 CDT 2008
from numpy import *
def distance(v1,v2):
return sqrt(((v2-v1)**2).sum())
def findmatch(wts,inputwt):
dist2 = ((wts-inputwt)**2).sum(axis=1)
idx = argmin(dist2)
return idx, sqrt(dist2[idx])
Nadav
-----הודעה מקורית-----
מאת: numpy-discussion-bounces@scipy.org בשם wilson
נשלח: ד 23-אפריל-08 08:00
אל: numpy-discussion@scipy.org
נושא: [Numpy-discussion] finding minimum distance using arrays
hi
i wrote a function to find euclidian distance between two vectors and
applied it to the rows of a 2d array of floats as below
from math import sqrt
from numpy import array,sum
def distance(vec1, vec2):
return sqrt(sum([(x-y)**2 for x,y in zip(vec1, vec2)]))
def findmatch(wts,inputwt):
mindist=99.0
index=0
for i in range(len(wts)):
d=distance(wts[i],inputwt)
if d == 0.0:
mindist=d
index=i
break
elif d < mindist:
mindist=d
index=i
return index,mindist
inputwt is a 1 dim array,wts is a 2d array .i want to find the
distance between inputwt and each row of wts...I used mindist=99.0 as
an improbable value for distance for the sample float arrays which i
used to test it...
i am not sure if this is the proper way to write this function..can
someone suggest how i can make it better?
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 3453 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080423/58c12076/attachment.bin
More information about the Numpy-discussion
mailing list