-1

If I have:

x = np.asarray([[1,2],[3,4],[5,6]])

And I would like to create:

y = np.asarray([1,4,5])

In order to do this, I built an array as follows:

inds = np.asarray([[0,0],[1,1],[2,0]])

And I passed it to x as follows:

y = x[inds]


This does not yield the elements indexed by the rows in inds. How do I achieve this functionality in either this fashion, or a fashion very similar to this?

Chris
  • 28,822
  • 27
  • 83
  • 158

1 Answers1

1

This is what advanced indexing for; Extract the row index and column index into two separate arrays and use them to subset the array:

x[inds[:,0], inds[:,1]]
# array([1, 4, 5])

Psidom
  • 209,562
  • 33
  • 339
  • 356