使用numpy数组循环遍历具有特定大小的较小矩阵的n x n矩阵 [英] Looping through n x n matrix with a smaller matrix of certain size with numpy arrays
问题描述
我目前遇到以下问题,给定一个数组,为简单起见,我们说一个4 x 4数组(我实际上使用的是512 x 512)
I'm currently having the following issue, given an array let's say for simplicity a 4 x 4 array (I'm actually working with 512 x 512 )
X = np.array([[3, 5, 2, 4],
[7, 6, 8, 8],
[1, 6, 7, 7],
[2, 1, 3, 4]])
我希望以可以将新数组保存为
I would like to loop/slide around the array in a way that I can save new arrays in the form
np.array([3,5],[7,6]),np.array([2,4],[8,8]),np.array([1,6],[2,1]),np.array([7,7],[1,4])
等(理想情况下,我可以选择滑动"窗口的步长和大小).我也想根据某些条件选择这些数组,请参见下文.
np.array([3,5],[7,6]), np.array([2,4], [8,8]), np.array([1,6],[2,1]), np.array ([7,7],[1,4])
and so on (Ideally that I could choose the step and the size of my "sliding" window). Also I would like to select these arrays according to some conditions , see below.
目前,我通过切片(参见代码)设法完成了几乎所有工作.这样可以按照我想要的步骤进行正确的切片,然后使用 itertools
模块,我可以遍历所有列表列表,对小于某个值的元素进行计数并保存.我不能做的是将所有这些新列表之间的索引链接到主矩阵.因此,我正在考虑将所有内容移至 numpy
数组,根据我的理解,它们应该在计算方面更加高效,我想应该可以解决索引问题.现在唯一的问题是我不知道要为任意 n x n
矩阵解决此问题.
For the moment I managed to do almost everything by slicing (see code) my matrix. This gives the correct slicing with the step which I want, then by using the itertools
module I can loop over all my list of lists, count the elements smaller than a certain value and save them.
What I cannot do is to link the indices between all these new lists together to the main matrix.
For this reason, I was thinking to move everything to numpy
arrays which should be (in my understanding) much more efficient in terms of computing and I guess should solve my issue of indices. Now the only problem is that I don't know to solve this problem for an arbitrary n x n
matrix.
X = np.array([[3, 5, 2, 4],
[7, 6, 8, 8],
[1, 6, 7, 7],
[2, 1, 3, 4]])
width = len(m[0])
height = len(m)
height = 2 # size of the matrix sliding
width = 2
for i in range(0, height - slice_y + 1,2):
for j in range(0, width - slice_x + 1,2):
Appended.append(
[
[m[a][b] for b in range(j, j + slice_x)]
for a in range(i, i + slice_y)
]
)
理想情况下,我想要的是通用矩阵N x N,但目前也仅像示例中那样以数组形式获得输出:
Ideally what I would like is for a general matrix N x N but for the moment also only like in the example to get the output as a form of arrays like:
np.array([3,5],[7,6]) . np.array ([2,4], [8,8]) , np.array ([1,6],[2,1]), np.array ([7,7],[1,4])
再说一次,发现例如数组 np.array([2,4],[8,8])
具有比 7
大的两个元素,并且sum大于 20
来保存此数组相对于我的初始矩阵的坐标.因此,保存索引对 X [0] [2]
, X [0] [3]
, X [1] [2]
, X [1] [3]
或至少第一个 X [0] [2]
,因此通过了解窗口"的步骤,我可以通过建立索引来访问我的子数组我的主要矩阵.
And let's say once found that e.g the array np.array([2,4], [8,8])
has two elements bigger than 7
and the sum is more than 20
to save the coordinates of this array with respect of my initial matrix. So saving the indices couples X[0][2]
, X[0][3]
, X[1][2]
, X[1][3]
or at least the first X[0][2]
so by knowing the step of my "window" I can access my subarrays by indexing my main matrix.
推荐答案
您显然可以直接切片numpy数组
You can apparently slice numpy arrays directly
X = np.array([[3, 5, 2, 4],
[7, 6, 8, 8],
[1, 6, 7, 7],
[2, 1, 3, 4]])[0:2,0:2]
在您的情况下,我将生成要使用的子矩阵的边缘索引的列表.然后使用它来生成子矩阵列表,然后使用它来基于子矩阵生成真值或假值列表.然后,您可以使用该true/false值列表来修剪您的初始索引列表.您也可以完全不存储子矩阵来执行此操作.
I would in your case generate a list of the indices of the edges of the submatrix you're going to use. Then use that to generate the list of submatricies then use that to generate a list of true or false values based on the submatricies. Then you can use that list of true/false values to prune your initial list of indicies. You could also do this without storing the submatricies at all.
indicies= [((i,i+s_width),(j,j+s_height)) for i in range(0,width-s_width) for j in range(0,height-s_height)]
这篇关于使用numpy数组循环遍历具有特定大小的较小矩阵的n x n矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!