如何将矩阵拆分为较小的子矩阵?

时间:2016-10-15 03:14:26

标签: matlab matrix reshape

我有这个矩阵

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

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

2 个答案:

答案 0 :(得分:3)

我认为这就是你要找的东西。

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

答案 1 :(得分:2)

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

的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
}