简体   繁体   中英

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.

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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