slice elements from 3d numpy array using two 1d numpy integer arrays as column and depth indices

16 views Asked by At

I have a 3d numpy array A (shape: NxMxK), two 1d integer arrays B and C (shapes: 1xN). Array B values are integers in 0 to M-1 (including endpoints) and array C values are integers in 0 to K-1 (including endpoints). How can I return the values of 3d array A[n,m,k] where, per each element index h of equal-shape arrays B and C, m is array B's value at index h and k is array C's value at index h? For instance:

import numpy as np
N, M, K = 3, 2, 4
A=np.random.uniform(100,120, 3*2*4).reshape(N,M,K) #generates 24 random nubmers in [100,120) and shapes it as a 3d array of 3x2x4.
B=np.random.randint(0, M, N) #generates N=3 random integers in [0,M=2)
C=np.random.randint(0, K, N)#generates N=3 random integers in [0,K=4)

# I want to use B as the index m and C as index k of A so the an array D = np.array([ A[0, B[0], C[0]],A[1, B[1], C[1]], ..., A[N-1, B[N-1], C[N-1]]]) is returned.
0

There are 0 answers