【问题标题】:How to cut a matrix in Matlab?Matlab中如何截取矩阵?
【发布时间】:2015-01-26 00:32:36
【问题描述】:

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

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];

【问题讨论】:

  • 这些是确切的尺寸,还是可以改变?基本上你想要做的是组合索引在 3 的跳跃中变化的行......?
  • This 可能会有所帮助

标签: matlab matrix reshape


【解决方案1】:

您所需要的只是一些 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,[])

【讨论】:

    【解决方案2】:

    这会建立一个线性索引来重新排列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, [])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多