[Numpy-discussion] How to get rid of the loop?

Alan G Isaac aisaac at american.edu
Sat Nov 7 14:04:28 EST 2009


On 11/7/2009 1:51 PM, Stas K wrote:
> Can I get rid of the loop in this example? And what is the fastest way
> to get v in the example?
>
> ar = array([1,2,3])
> for a in ar:
>      for b in ar:
>          v = a**2+b**2


>>> a2 = a*a
>>> np.add.outer(a2,a2)
array([[ 2,  5, 10],
        [ 5,  8, 13],
        [10, 13, 18]])

hth,
Alan Isaac




More information about the NumPy-Discussion mailing list