我有这个矩阵
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
感谢您的每一个回复...
答案 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!如果您需要将它们分开,请将它们放在三维矩阵中的不同层中,或使用单元格阵列。
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
}