[Numpy-discussion] Problem migrating PDL's index() into NumPy

Miroslav Sedivy miroslav.sedivy@weather-consult....
Wed Mar 17 10:39:21 CDT 2010


>>>> n0 = 5  # number of rows
>>>> B = np.ones((n0,3))*np.arange(3)
>>>> A = np.random.randint(3,size=(n0,3))
>>>> C = B[np.arange(n0)[:,None],A]
>>>> assert (A == C).all()
>>>> A
> array([[2, 0, 1],
>        [2, 0, 1],
>        [2, 1, 2],
>        [0, 0, 2],
>        [2, 0, 0]])
>>>> C
> array([[ 2.,  0.,  1.],
>        [ 2.,  0.,  1.],
>        [ 2.,  1.,  2.],
>        [ 0.,  0.,  2.],
>        [ 2.,  0.,  0.]])
> 
> Josef


Thank you, Josef, now it works! I had a problem with the shape of my 
arrays. When I transposed them correctly, your solution has worked!

Miroslav


More information about the NumPy-Discussion mailing list