如何提取矩阵中非连续的行和列?

3

如何从矩阵中提取非连续的行和列。例如,在这个矩阵中如何提取第1、2和4行以及第1、2和4列?

import numpy as np

a = np.matrix([[1, 2, 3, 4, 5],
               [6, 7, 8, 9, 10],
               [11, 12, 13, 14, 15],
               [16, 17, 18, 19, 20],
               [18, 19, 20, 21, 22]])

所以新矩阵应该是:
b = ([[7, 8 , 10],
     [12, 13, 15],
     [19, 20, 22]])

3
你的意思是“非连续通话”是什么? - user17242583
你的问题需要更多的解释。当你说不连续时,是指你始终只想要第1、2和第4行/列吗? - Mr. Techie
非连续的,因为2和4之间有一个“间隙”。 - user18247408
这被称为“高级索引”。官方文档是您的朋友:https://numpy.org/doc/stable/user/basics.indexing.html#advanced-indexing - hpaulj
1个回答

1
在由hpaulj链接的文档部分中(点击此处),请参见以From a 4x3 array the corner elements should be selected using advanced indexing.开头的示例。
具体来说,从This broadcasting can also be achieved using the function ix_:开始的段落。
在你的情况下,行是[1, 2, 4],列也是如此。
rows = np.array([1, 2, 4], dtype=np.intp)
columns = np.array([1, 2, 4], dtype=np.intp)
b = a[np.ix_(rows, columns)]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接