1

I have a matrix in numpy

A = [[1,2,3],
     [4,5,6],
     [7,8,9]]

and a plain array

B = [2,1,0]

I want to compress the matrix into a single column such that for the ith row I want to pick the B[i]th column and put it in the column vector

So I want an elegant/vectorized implementation to get

Ans = [3,5,7]
Naman
  • 350
  • 4
  • 18

1 Answers1

1

Try this:

In [32]: A[np.arange(A.shape[0]), B]
Out[32]: array([3, 5, 7])
MaxU - stop genocide of UA
  • 191,778
  • 30
  • 340
  • 375