Numpy 3D indexing sepecific column elements

Viewed 26

I have a multidimensional (3D) array like the following:

>>>arr=np.array([[[0, 0],
               [1, 1]],

              [[2, 0],
               [3, 1]],

              [[3, 0],
               [4, 1]]])

I would like to obtain for each of the 2d submatrices a the element of column 0 located in a certain row, being the value that selects the row a value associated with each submatrix, that is, I would like to achieve:

>>>arr[:,[0,1,0],0]
array([0,3,3])

However, with the above command what I get is:

>>>arr[:,[0,1,0],0]
array([[0, 1, 0],
       [2, 3, 2],
       [3, 4, 3]])

Per the documentation, I was able to achieve the goal using the following command:

>>>arr[range(arr.shape[0]),[0,1,0],0]
array([0,3,3])

But I would like to know if there is a better way where I don't need to pass a list with all the indices for the first element of the indexing, like in the first example.

0 Answers
Related