Let's pretend that
import numpy as np
np.random.seed(0)
n,m,k = 2,3,5
A = np.arange(n*m*k,0,-1).reshape((n,m,k))
print(A)
B = np.random.randint(k, size=(n,m))
print(B)
To create this array,
print(A.reshape(-1, k)[np.arange(n * m), B.ravel()])
# [26 25 17 12 7 4]
as an array nxmusing fantasy indexing:
i,j = np.ogrid[0:n, 0:m]
print(A[i, j, B])
source
share