Indexing one array by another in numpy

前端 未结 4 1009
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 05:12

Suppose I have a matrix A with some arbitrary values:

array([[ 2, 4, 5, 3],
       [ 1, 6, 8, 9],
       [ 8, 7, 0, 2]])

A

4条回答
  •  猫巷女王i
    2020-11-22 05:41

    I know this is an old question, but another way of doing it using indices is:

    A[np.indices(B.shape)[0], B]
    

    output:

    [[2 2 4 5]
     [1 9 8 6]
     [2 0 7 8]]
    

提交回复
热议问题