Matlab 之 im2col 【转】

函数原型: B=im2col(A,[m n],block_type)

功能:将矩阵A分为m×n的子矩阵,再将每个子矩阵作为B的一列

(1)当block_type为distinct时,将A分解为互不重叠的子矩阵,并转换成列。若不足m×n,以0补足。

例:                A=

   15   36   42   40   67   88   32   50

   59   74   27   81   44   59   53   15

   26   39   20   76   83   15    9    5

    4   68   82   38   77   20   11   85

   75   70   43   22   17   41   14   56

   24   44   89   79   86   75   68   93

   44    2   39   95   99   83   50   70

   69   33   77   33   51   79   19   58

B=im2col(A, [3, 3], 'distinct')

15        44   40   38   95   32   11   50

        59   75   69   81   22   33   53   14   19

      26   24    0     76   79    0    9   68      0

        36   68    2     67   77   99   50   85   70

         74   70   33    44   17   51   15   56   58

        39   44    0     83   86    0     5    93     0

        42   82   39    88   20   83     0     0     0

        27   43   77    59   41   79     0     0     0

        20   89    0     15   75    0     0      0     0

(2)当block_type为sliding时,将A分解平移一行(列)的mxn的子矩阵,并转换成列。
例:                             A=

   21   11   62

   95   14   57

   8    17    5

B=im2col(A, [2, 2], 'sliding')为

   21   95   11   14

   95    8   14   17

   11   14   62   57

   14   17   57    5

此时,根据A为3×3和子块为2×2,最多可构成B的列数为((3-2)+1)×((3-2)+1)=4,列长(列数)为2×2=4

注:对于‘sliding’类型,如果设A为m×n的,[p q]为block,则最后矩阵的行(或列)的大小为:

              (m-p+1) x (n-q+1)

应用实例:对矩阵进行m×n分块,用子块的和生成新矩阵。

B = im2col(A,[m n],'sliding');

C = reshape(sum(B),mm-m+1,nn-n+1);

原文地址:https://www.cnblogs.com/rong86/p/3557193.html