Matlab重塑横向猫
嗨,我想重塑一个矩阵,但重塑命令不按照我想要的方式排列元素。 我有矩阵元素:
A B
C D
E F
G H
I K
L M
并希望将其重塑为:
A B E F I K
C D G H L M
所以我知道我想要有多少行(在这种情况下是2),并且2行的所有“组”应该水平地附加。 这可以在没有for循环的情况下完成吗?
  你可以做两次reshape和一次permute 。  设n表示每个组的行数: 
y = reshape(permute(reshape(x.',size(x,2),n,[]),[2 1 3]),n,[]);
  有3列的例子, n=2 : 
>> x = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
x =
     1     2     3
     4     5     6
     7     8     9
    10    11    12
>> y = reshape(permute(reshape(x.',size(x,2),n,[]),[2 1 3]),n,[])
y =
     1     2     3     7     8     9
     4     5     6    10    11    12
单元阵列方法 -
mat1 = rand(6,2) %// Input matrix
nrows = 3; %// Number of rows in the output
[m,n] = size(mat1);
%// Create a cell array each cell of which is a (nrows x n) block from the input
cell_array1 = mat2cell(mat1,nrows.*ones(1,m/nrows),n);  
%// Horizontally concatenate the double arrays obtained from each cell
out = horzcat(cell_array1{:})
代码运行时输出 -
mat1 =
    0.5133    0.2916
    0.6188    0.6829
    0.5651    0.2413
    0.2083    0.7860
    0.8576    0.3032
    0.1489    0.4494
out =
    0.5133    0.2916    0.5651    0.2413    0.8576    0.3032
    0.6188    0.6829    0.2083    0.7860    0.1489    0.4494
