我需要将3D数组s
转换为2D数组sReshape
,其方式是将第三维的每个切片简单地放在第一个切片的2D数组的行之下。
以下是示例以及预期的解决方案:
s = reshape((1:30),[5,3,2]);
sReshape = ???
resultExpected = [(1:5),(16:20) ; (6:10),(21:25) ; (11:15),(26:30)]';
isequal(sReshape, resultExpected)
答案 0 :(得分:2)
您可以在重新塑造之前使用permute
在第二维和第三维之间切换:
s = reshape((1:30),[5,3,2]);
% switch between the 2nd and third dimensions
y = permute(s,[1 3 2]);
% reshape into 3 columns matrix
sReshape = reshape(y,[],3);
resultExpected = [(1:5),(16:20) ; (6:10),(21:25) ; (11:15),(26:30)]';
isequal(sReshape, resultExpected)