提取某些行/列值的子矩阵 [英] Extract submatrix at certain row/col values
本文介绍了提取某些行/列值的子矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例:
import numpy as np
# create matrix
mat_A = np.arange(100).reshape((10, 10))
row = 5
col = 5
# Build 3x3 matrix around the centre point
matrix_three = ((row - 1, col - 1),
(row, col - 1),
(row + 1, col - 1),
(row - 1, col),
(row, col), # centre point
(row + 1, col),
(row - 1, col + 1),
(row, col + 1),
(row + 1, col + 1))
list_matrix_max_values = []
for loc in matrix_three:
val = mat_A[loc[0]][loc[1]]
list_matrix_max_values.append(val)
submatrix = np.matrix(list_matrix_max_values)
print(submatrix)
退货:
[[44 54 64 45 55 65 46 56 66]]
例如,如果我想要在行/列索引定义的单元格周围提取一个5x5矩阵,我如何做同样的事情?
提前感谢!
推荐答案
S=3 # window "radius"; S=3 gives a 5x5 submatrix
mat_A[row-S+1:row+S,col-S+1:col+S]
#array([[33, 34, 35, 36, 37],
# [43, 44, 45, 46, 47],
# [53, 54, 55, 56, 57],
# [63, 64, 65, 66, 67],
# [73, 74, 75, 76, 77]])
这篇关于提取某些行/列值的子矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文