简体   繁体   中英

Numpy: Indexing 3D matrix using 1D array

arr = np.arange(12).reshape((3, 2, 2))
indices = np.array([0, 1, 1])

expected_outcome = np.array([[0, 1], [6, 7], [10, 11]])

I'm trying to index this array of shape (3,2,2) with an array of shape (3) containing the y-index of the value I want to get. I tried to make it work with for in statement, but is there an elegant way to do it with numpy?

So you want arr[0,0,:], arr[1,1,:], arr[2,1,:] ?

How about

In [179]: arr[[0,1,2], [0,1,1]]
Out[179]: 
array([[ 0,  1],
       [ 6,  7],
       [10, 11]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM