import numpy as np
A= array([[ 1,  2,  3,  4,  5,  6,  7,  8],
              [ 9, 10, 11, 12, 13, 14, 15, 16],
              [17, 18, 19, 20, 21, 22, 23, 24],
              [25, 26, 27, 28, 29, 30, 31, 32],
              [33, 34, 35, 36, 37, 38, 39, 40],
              [41, 42, 43, 44, 45, 46, 47, 48],
              [49, 50, 51, 52, 53, 54, 55, 56],
              [57, 58, 59, 60, 61, 62, 63, 64]])


我想按此顺序抓取(0,4,5,7)行和col
所以o / p看起来像

A=
    1    5    4    8
   33   37   36   40
   25   29   28   32
   57   61   60   64


我尝试了这个A [(0,4,5,7),(0,4,5,7)],但是它给了我错误。

注意:我想将其切成相同的矩阵。

最佳答案

正如您在上一条评论中指出的那样,您还可以使用Numpy advanced indexing

J = np.repeat([[0,4,3,7]], 4, axis=0)
B = A[J.T, J]
>>> B
array([[ 1,  5,  4,  8],
       [33, 37, 36, 40],
       [25, 29, 28, 32],
       [57, 61, 60, 64]])

关于python - python中矩阵的索引和切片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49016859/

10-14 16:05