如何在Matlab中剪切矩阵?

时间:2014-11-27 11:56:46

标签: matlab matrix reshape

我想将矩阵A转换为矩阵B而不使用Matlab中的单元格(例如mat2cell),其中

A=[1 2 3; 
   4 5 6; 
   7 8 9; 
   10 11 12; 
   13 14 15; 
   16 17 18; 
   19 20 21; 
   22 23 24; 
   25 26 27];

B=[1 2 3 10 11 12 19 20 21;
   4 5 6 13 14 15 22 23 24; 
   7 8 9 16 17 18 25 26 27];

2 个答案:

答案 0 :(得分:2)

您只需要一些 reshape + permute 魔法 -

N = 3;  %// Cut after every N rows and this looks like the no. of columns in A
B = reshape(permute(reshape(A,N,size(A,1)/N,[]),[1 3 2]),N,[])

答案 1 :(得分:0)

这会构建一个线性索引来重新排列A的条目,然后重新整形为所需的矩阵B

m = 3; %// cut size in rows of A. Assumed to divide size(A,1)
n = size(A,2);
p = size(A,1);
ind = bsxfun(@plus, ...
      bsxfun(@plus, (1:m).', (0:n-1)*p), permute((0:p/m-1)*m, [1 3 2]));
B = reshape(A(ind(:)), m, [])