How to access numpy array with a set of indices stored in another numpy array?
I have a numpy array which stores a set of indices I need to access another numpy array.
I tried to use a
forloop but it doesn't work as I expected.
The situation is like this:
>>> a array([[1, 2], [3, 4]]) >>> c array([[0, 0], [0, 1]]) >>> a[c] array([[1, 2], [1, 2]]) >>> a[0,0] # the result I want 1
Above is a simplified version of my actual code, where the
carray is much larger so I have to use a
forloop to get every index.
Convert it to a
>>> a[tuple(c)] 1
arrayindices trigger advanced indexing.
tuples are (mostly) basic slicing.
awith columns of
cby passing the first column as row's index and second one as column index:
In : a[c[:,0], c[:,1]] Out: array([1, 2])