【问题标题】:How can I split a matrix into smaller sub-matrices?如何将矩阵拆分为更小的子矩阵?
【发布时间】:2016-10-15 03:14:25
【问题描述】:

我有这个矩阵

x =

 5     5     3     3     2    
 5     5     2     2     2     
 0     5     5     4     4     
 0     0     0     0     0    
 0     0     0     0     0     
 2     0     3     0     0     

如何将该矩阵重塑为:

x1 = 5     5     3     3     2    
     5     5     2     2     2
x2 = 0     5     5     4     4     
     0     0     0     0     0
x3 = 0     0     0     0     0     
     2     0     3     0     0

感谢您的每一个回复...

【问题讨论】:

    标签: matlab matrix reshape


    【解决方案1】:

    我想这就是你要找的。​​p>

    x1 = x(1:2,:)
    x2 = x(3:4,:)
    x3 = x(5:6,:)
    

    【讨论】:

    • 注意:这在 MATLAB 中几乎不是一个好主意。此外,如果尺寸发生变化,该解决方案的扩展也很麻烦。
    • 我确实改变了使用变量的行范围,所以尺寸不会改变,所以我给代码一些组合谢谢你的帮助,我很感激并会尝试你的代码@Stewie Griffin :)跨度>
    【解决方案2】:

    Don't do this! 这绝不是个好主意!您是否计划使用combining this with eval? Don't! 如果您需要将它们分开,请将它们放在 3 维矩阵中的不同层中,或者使用单元格数组。

    3D 阵列:

    permute(reshape(x.', 5, 2, []), [2 1 3])
    ans =
    ans(:,:,1) =
       5   5   3   3   2
       5   5   2   2   2
    ans(:,:,2) =
       0   5   5   4   4
       0   0   0   0   0
    ans(:,:,3) =
       0   0   0   0   0
       2   0   3   0   0 
    

    细分

    permute(reshape(x.',5,2,[]), [2 1 3])
                    x.'                    % Transpose x
            reshape(x.',5,2,[])            % Reshape x.' to have 5 rows, 2 cols, []-layers
    permute(reshape(x.',5,2,[]), [2 1 3])  % Permute the new 3-dimensional matrix
                                           % permute(mat,[2 1 3]) will transpose each layer of
                                           % the matrix giving us the result we want
    

    元胞数组:

    mat2cell(x, [2, 2, 2])
    ans = 
    {
      [1,1] =
         5   5   3   3   2
         5   5   2   2   2
      [2,1] =
         0   5   5   4   4
         0   0   0   0   0
      [3,1] =
         0   0   0   0   0
         2   0   3   0   0
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-16
      • 2020-05-13
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      相关资源
      最近更新 更多