0

I'm trying to index N values in a NxM numpy array, 1 in each row. The Indices are ordered in a list.

The way to normally index a multidimensional array is

import numpy as np

a = np.arange(12).reshape(3,4)
a[2,3] # 11

for multiple indices:

a[[1,1],[0,2]] # array([4,6])

So, for my question, I'm trying to achieve this:

i = [1,2,1]
a[np.arange(3), i] # array([1,6,9])

I'm wondering if the row indices can be given in an implicit way. I thought that the semicolon operator would help out, but it gives me the complete columns.

joepjp
  • 79
  • 3
  • 1
    what is the question? ... whether you can write the last line in your code without `np.arange`? – plonser Apr 29 '15 at 10:10
  • yes, [`np.choose`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.choose.html) appears to be the solution. Thanks to @ajcr for recognizing the duplicate. – joepjp Apr 29 '15 at 10:27

0 Answers0