获取所有子矩阵

kay*_*kay 6 matlab vectorization submatrix

我有一个N×M矩阵,m如:

 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15 16
Run Code Online (Sandbox Code Playgroud)

我希望使用for循环得到大小为P×Q(P,Q为奇数)的所有子矩阵.

结果s应该是P×Q×((N-P + 1)·(M-Q + 1))矩阵.

例如,如果P = Q = 3:

s(:,:,1) = [1 2 3;  5  6  7;  9 10 11]
s(:,:,2) = [2 3 4;  6  7  8; 10 11 12]
s(:,:,3) = [5 6 7;  9 10 11; 13 14 15]
s(:,:,4) = [6 7 8; 10 11 12; 14 15 16]
Run Code Online (Sandbox Code Playgroud)

KQS*_*KQS 6

im2col 可以帮到你这里:

m =
     1     2     3     4
     5     6     7     8
     9    10    11    12
    13    14    15    16

>> P = 3; Q = 3;
>> columnized = im2col(m,[P Q],'sliding');
>> nMatrices = size(columnized,2);
>> s = reshape(columnized, [P Q nMatrices])

s(:,:,1) =
     1     2     3
     5     6     7
     9    10    11
s(:,:,2) =
     5     6     7
     9    10    11
    13    14    15
s(:,:,3) =
     2     3     4
     6     7     8
    10    11    12
s(:,:,4) =
     6     7     8
    10    11    12
    14    15    16
Run Code Online (Sandbox Code Playgroud)

im2col使用该'sliding'选项查找所有重叠的子矩阵,并将每个子矩阵作为(P·Q) - 元素列向量返回columnized.为了将这些转化为矩阵,我们reshape将此(P·Q)×((N-P + 1)·(M-Q + 1))矩阵转换为P×Q×((N-P + 1)·(M -Q + 1))一个.