python - numpy 从二维矩阵获取子矩阵

标签 python numpy matrix

我有一个 400 列 1000 行的矩阵

从第 10 行第 30 列开始到第 390 行第 960 列从中提取子矩阵的最佳方法是什么?

嗡嗡声

最佳答案

如果您的矩阵名为 arr,请使用:

arr[10:391,30:961]

记住 python 是零索引的!

例如,在这个 10 行 5 列的矩阵上:

>>> arr
array([[0.41296756, 0.14399754, 0.12606098, 0.61114244, 0.83243229],
       [0.91409281, 0.28715535, 0.63134933, 0.49837152, 0.15927328],
       [0.89021618, 0.16226017, 0.75576869, 0.4949617 , 0.86590637],
       [0.7684741 , 0.82399817, 0.48887077, 0.12121645, 0.64524496],
       [0.37356536, 0.4548252 , 0.09608423, 0.49361169, 0.12475545],
       [0.74875009, 0.16289433, 0.84103655, 0.09670591, 0.34296308],
       [0.0697272 , 0.87959546, 0.2928768 , 0.53756533, 0.96180757],
       [0.3495039 , 0.09705972, 0.05581994, 0.95367967, 0.94351724],
       [0.03739738, 0.65043277, 0.94139692, 0.82945502, 0.51765616],
       [0.07702866, 0.57164611, 0.55674663, 0.21082212, 0.76406612]])

我可以使用以下方法选择第 5-7 行、第 1-3 列:

>>> arr[5:8,1:4]
array([[0.16289433, 0.84103655, 0.09670591],
       [0.87959546, 0.2928768 , 0.53756533],
       [0.09705972, 0.05581994, 0.95367967]])

关于python - numpy 从二维矩阵获取子矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52213628/

相关文章:

Python 类内函数递归

python - 将 Matlab 数值热传导模型转换为 Python

python - 在 numpy 中使用多项式时如何向量化预测

python - 在没有循环的情况下在 Numpy 中切片一维数组

javascript - Matrix3d propertyValues 及其他

python - 属性错误 : 'NoneType' object has no attribute 'lower' python

python - 正则表达式 Python 数据操作——NoneType 对象

python - 将文件打开到数组中,搜索字符串并返回值

c++ - 如何: return multidimensional vector (Matrix) from function - using header

python - 通过命令行使用 numpy