I'm trying to index a multidimensional array P with another array indices. which specifies which element along the last axis I want, as follows:
import numpy as np
M, N = 20, 10
P = np.random.rand(M,N,2,9)
# index into the last dimension of P
indices = np.random.randint(0,9,size=(M,N))
# I'm after an array of shape (20,10,2)
# but this has shape (20, 10, 2, 20, 10)
P[...,indices].shape
How can I correctly index P with indices to get an array of shape (20,10,2)?
If that's not too clear: For any i and j (in bounds) I want my_output[i,j,:] to be equal to P[i,j,:,indices[i,j]]
iandjI wantmy_output[i,j,:]to be equal toP[i,j,:,indices[i,j]]