Numpy operations for generating a 2D array from a 3D array


I have an array A with shape [2, 512, 21128] and another list [7,41]

I want to create a new array with shape [2,21128] from A[0,7,:] and A[1,41,:].

I can use a loop to do such things, is there any convenient way to do this? I try to find it online but I actually do not know how to ask this question.

>Solution :

You can use advanced indexing:

arr = np.random.rand(2, 512, 21128)
out = arr[[0, 1], [7, 41]]


(2, 21128)

Leave a Reply Cancel reply