简体   繁体   English

MATLAB:将3D转换为2D(并置)

[英]MATLAB: Transform 3D into 2D (concatenation)

I need to transform a 3D array s into a 2D array sReshape in a way where every slice of the third dimension will simply be put below the rows of the first slice's 2D array. 我需要将3D数组s转换为2D数组sReshape ,其方式是将第三维的每个切片简单地放在第一个切片的2D数组的行下方。

Here's the example as well as the expected solution: 这是示例以及预期的解决方案:

s = reshape((1:30),[5,3,2]);
sReshape = ???

resultExpected = [(1:5),(16:20) ; (6:10),(21:25) ; (11:15),(26:30)]';
isequal(sReshape, resultExpected)

you can use permute to switch between the second and third dimensions before reshaping: 您可以在重塑之前使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM