简化行和列提取,numpy

问题描述:

我希望使用单个奇特"切片从矩阵中提取行和列,这可能吗?

I wish to extract rows and columns from a matrix using a single "fancy" slice, is this possible?

m = matrix([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]])

我的目标是

matrix([[1, 3],
        [7, 9]])

哪里有我想要的物品清单

Where I have a list of the items I want

d = [0,2]

我可以通过以下方式实现功能

I can achieve the functionality by

m[d][:,d]

但是有没有更简单的表达方式?

But is there a simpler expression?

您可以使用

You can do this using numpy.ix_:

m = matrix([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]])

d = [0,2]
print m[ix_(d,d)]

它将发出:

[[1 3]
 [7 9]]