矩阵的滑动窗口求和

2024-02-27

我有一个 50x50 矩阵,我想对每个 10x10(或另一个设定大小值 - 始终是正方形)重叠网格中的值求和,即:

为了清楚起见,重叠的窗口仅以对角线显示。我尝试做的第一个任务是定义每个窗口的坐标:

win=10;
start = [1,10,1,10];
for y=1:(50-win)
    for g=1:(50-win)
        tmp = [start(g,1)+1,start(g,2)+1,start(end,3),start(end,4)];
        start = [start;tmp];
    end
    start(end+1,1:4) = [1,10,1+y,10+y];
end

然后我会循环遍历坐标列表,使用sum以及每个窗口的逻辑索引。

问题#1: 上面的代码不是特别雄辩。有人可以展示一种更“MATLABesque”的方法或更简洁的方法吗?

问题#2:然后我想在矩阵中定义一个特定的坐标(索引),例如m(26,26)并获取包含该坐标的所有窗口的列表。但我不知道该怎么做。有人能告诉我怎么做吗?


问题#1

我能想到的最像 Matlab 的方法是二维卷积(conv2 https://www.mathworks.com/help/matlab/ref/conv2.html)(正如我现在看到的@rahnema1 评论的那样):

M = randi(9, 5, 5); % input: square matrix, arbitrary size
N = 3; % block size, assumed square, not larger than M
result = conv2(M, ones(N), 'valid');

同样,您可以使用最近推出的movsum https://www.mathworks.com/help/matlab/ref/movsum.html函数,两次(每个维度一次):

result = movsum(movsum(M, N, 1, 'Endpoints', 'discard'), N, 2, 'Endpoints', 'discard');

Example:

M =
     4     4     3     1     2
     2     8     7     1     6
     3     6     7     5     5
     6     5     4     8     1
     5     9     6     9     4

result =
    44    42    37
    48    51    44
    51    59    49

问题#2

最简单的方法(不是最有效的方法)是再次使用包含逻辑矩阵的卷积true在所需的位置和false否则,检查卷积不为零的地方:

in_coords = [3 4]; % example input coordinates
T = false(size(M)); % initiallize matrix containing false, same size as M
T(in_coords(1), in_coords(2)) = true; % true at the desired coordinates
C = conv2(T, ones(N), 'valid'); % this gives 1 for blocks affected by in_coords
[ii, jj] = find(C); % row and column indices of nonzero values 
out_coords = [ii jj]; % build result

在这个例子中,

out_coords =
     1     2
     2     2
     3     2
     1     3
     2     3
     3     3
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

矩阵的滑动窗口求和 的相关文章

随机推荐