如何使用MATLAB水平重塑矩阵

时间:2014-11-09 11:08:07

标签: matlab reshape

我有A的矩阵size(4,192)。它由12个水平对齐的size(4,4)矩阵组成。我想用size(12,16)得到矩阵B. B必须如下: 假设

A=[y1,y2,y3,...,y12]

其中yn是4 * 4矩阵。然后,

B=[y1,y4,y7,y10;
   y2,y5,y8,y11;
   y3,y6,y9,y12]

使用MATLAB有效/快速(无循环)方式吗?

1 个答案:

答案 0 :(得分:2)

您可以尝试以下代码:

ys1 = 2; % size(1) from submatrix (for the following example, use ys1 = 4 for the actual problem)
ys2 = 2; % size(2) from submatrix (for the following example, use ys2 = 4 for the actual problem)

ns1 = 3; % size(1) of final matrix in terms of submatrix (3 rows)
ns2 = 4; % size(2) of final matrix in terms of submatrix (4 columns)

temp = reshape(A,ys1,ys2,ns1,ns2);
B = reshape(permute(temp,[1 3 2 4]),ys1*ns1,ys2*ns2);

示例:

A = [11 12   21 22   31 32   41 42   51 52   61 62   71 72   81 82   91 92   101 102   111 112   121 122;
     13 14   23 24   33 34   43 44   53 54   63 64   73 74   83 84   93 94   103 104   113 114   123 124];

B =

    11    12    41    42    71    72   101   102
    13    14    43    44    73    74   103   104
    21    22    51    52    81    82   111   112
    23    24    53    54    83    84   113   114
    31    32    61    62    91    92   121   122
    33    34    63    64    93    94   123   124