Skip to content Skip to sidebar Skip to footer

Indexing Multidimensional Arrays With An Array

I have a multidimensional NumPy array: In [1]: m = np.arange(1,26).reshape((5,5)) In [2]: m Out[2]: array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 1

Solution 1:

Numpy is using your array to index the first dimension only. As a general rule, indices for a multidimensional array should be in a tuple. This will get you a little closer to what you want:

>>>m[tuple(p)]
array([9, 9])

But now you are indexing the first dimension twice with 1, and the second twice with 3. To index the first dimension with a 1 and a 3, and then the second with a 1 and a 3 also, you could transpose your array:

>>>m[tuple(p.T)]
array([ 7, 19])

Post a Comment for "Indexing Multidimensional Arrays With An Array"