如何将矩阵拆分为更小的子矩阵?
How can I split a matrix into smaller sub-matrices?
我有这个矩阵
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
感谢您的每一个回复...
我想这就是您要找的。
x1 = x(1:2,:)
x2 = x(3:4,:)
x3 = x(5:6,:)
Don't do this! It's never a good idea! Are you planning on 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
}
我有这个矩阵
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
感谢您的每一个回复...
我想这就是您要找的。
x1 = x(1:2,:)
x2 = x(3:4,:)
x3 = x(5:6,:)
Don't do this! It's never a good idea! Are you planning on 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
}