Convert cell arrays to 4-D array

12 views (last 30 days)
Wenyi Xiao
Wenyi Xiao on 22 Apr 2019
Edited: Matt J on 23 Apr 2019
WeChat Screenshot_20190422191521.png
I've got a 35*10 cell array, which contains multiple matrices with the same size(256*256). I want to convert it into a 4-D array(256*256*10*35).
  1 Comment
Stephen23
Stephen23 on 23 Apr 2019
Important note to future readers: the accepted answer mixes up the data!

Sign in to comment.

Accepted Answer

Matt J
Matt J on 22 Apr 2019
Edited: Matt J on 23 Apr 2019
Where C is your cell array:
M = reshape( cat(3,C{:}) , [256,256,10,35])
EDIT:
Or M = reshape( cat(3,C{:}) , [256,256,35,10]) ?
  4 Comments
Matt J
Matt J on 23 Apr 2019
Edited: Matt J on 23 Apr 2019
OK, I think you should allow for the possibility, though, that the OP had a typo and really meant to say that the final dimensions should be [256,256,35,10], since that requires the least data reorganization. In that case, my solution is valid with the following small change.
M = reshape( cat(3,C{:}) , [256,256,35,10])
It is also possible that the OP meant a 3rd alternative,
C=C.';
M = reshape( cat(3,C{:}) , [256,256,10,35])

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 22 Apr 2019
Where C is your cell array:
M = cell2mat(permute(C,[4,3,2,1]))
  2 Comments
Stephen23
Stephen23 on 23 Apr 2019
Edited: Stephen23 on 23 Apr 2019
@Wenyi Xiao: my pleasure.
You should know that the answer you accepted gives the data in an mixed up order.

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!