当需要从一个2维矩阵中切分出一个子矩阵时,给定了切分的index_list
import numpy as np
a = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
])
index_list = [1, 2, 3]
# 要得到[[6,7],[10,11]]
# 得不到对应的子矩阵
print(a[index_list, index_list])
# 切分子矩阵的正确姿势
print(a[index_list, :][:, index_list])
输出
[ 6 11 16]
[[ 6 7 8]
[10 11 12]
[14 15 16]]