# Convert cell arrays to 4-D array

12 views (last 30 days)
Wenyi Xiao on 22 Apr 2019
Edited: Matt J on 23 Apr 2019
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 CommentShow NoneHide None
Stephen23 on 23 Apr 2019
Important note to future readers: the accepted answer mixes up the data!

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]) ?
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])

### More Answers (1)

Stephen23 on 22 Apr 2019
Where C is your cell array:
M = cell2mat(permute(C,[4,3,2,1]))
##### 2 CommentsShow 1 older commentHide 1 older comment
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.