[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Need Help with Distance Algorithm in Numeric



Kamilche <klachemin@xxxxxxxxxxx>:
>
> Thanks, I appreciate that! :-) But even so, the output I'm getting is as 
> follows:
> 
> >>>
> [[10 10 10]
>  [10 10 10]
>  [10 10 10]]

Strange... Without checking *too* closely for errors in your code, I
simply replaced "from numarray import *" to "from Numeric import *"
and "second[...]" with "take(second, ...)" in my code (using Python
2.4 and Numeric 23.8), and I got

[[20 20 20]
 [10 10 10]
 [50 50 50]]

as expected.

Here is the code:

---------------------------------------------------------------------
from Numeric import *

def dist(a, b):
    return sqrt(sum((a - b)**2, -1))

first = array([[10, 20, 30],
               [ 0,  0,  1],
               [50, 60, 50]])

second = array([[10, 10, 10],
                [20, 20, 20],
                [30, 30, 30],
                [40, 40, 40],
                [50, 50, 50]])

print take(second, argmin(dist(first, second[..., NewAxis]), 0))
---------------------------------------------------------------------

-- 
Magnus Lie Hetland                   Fall seven times, stand up eight
http://hetland.org                                 [Japanese proverb]