Convert cell arrays to 4-D array

31 views (last 30 days)
Wenyi Xiao
Wenyi Xiao on 22 Apr 2019
Commented: debojit sharma on 17 Jun 2023
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
Stephen23
Stephen23 on 23 Apr 2019
Edited: Stephen23 on 23 Apr 2019
"I think it will -- because of the way cat is applied."
Nope, that is incorrect (for the reasons that I already explained in my previous comment). You could concatenate along any dimension >2 and it makes absolutely no difference to the linear-indexing order that your answer uses: this order will always mix up the data. Your answer is flawed without a transpose or a permute (simplest would be a transpose beforehand).
It very easy to demonstrate with a simpler example, which should also make the reason clear. Here I used a 3x5 cell array of 1x1 numeric arrays:
>> C = {11,12,13,14,15;21,22,23,24,25;31,32,33,34,45} % digits= row col
C = 11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
>> M = reshape(cat(3,C{:}),[1,1,5,3])
M =
ans(:,:,1,1) = 11
ans(:,:,2,1) = 21
ans(:,:,3,1) = 31
ans(:,:,4,1) = 12
ans(:,:,5,1) = 22
ans(:,:,1,2) = 32
ans(:,:,2,2) = 13
ans(:,:,3,2) = 23
ans(:,:,4,2) = 33
ans(:,:,5,2) = 14
ans(:,:,1,3) = 24
ans(:,:,2,3) = 34
ans(:,:,3,3) = 15
ans(:,:,4,3) = 25
ans(:,:,5,3) = 45
Here we can see for example, that along the 3rd dimesion M(1,1,:,1) (which has size 5) contains the values 11, 21, 31, 12, and 22. Your code has mixed up the elements of those two dimensions, exactly as I said it would: three elements from the first column, two elements from the second column! Lets look along the third dimension:
M(1,1,:,1) = 11,21,31,12,22
M(1,1,:,2) = 32,13,23,33,14
M(1,1,:,3) = 24,34,15,25,45
Try it on any non-square example and you will find that your code always mixes up the data.
Compare to my (correct) answer:
>> M = cell2mat(permute(C,[4,3,2,1]))
M =
ans(:,:,1,1) = 11
ans(:,:,2,1) = 12
ans(:,:,3,1) = 13
ans(:,:,4,1) = 14
ans(:,:,5,1) = 15
ans(:,:,1,2) = 21
ans(:,:,2,2) = 22
ans(:,:,3,2) = 23
ans(:,:,4,2) = 24
ans(:,:,5,2) = 25
ans(:,:,1,3) = 31
ans(:,:,2,3) = 32
ans(:,:,3,3) = 33
ans(:,:,4,3) = 34
ans(:,:,5,3) = 45
Along the 3rd dimension M(1,1,:,1) (which has size 5) are the elements 11, 12, 13, 14, 15, thus keeping exactly the sequence of the orginal cell array's 2nd dimension (which has size 5), even though its dimensions have been swapped. Lets look at all of the 3rd dimension:
M(1,1,:,1) = 11,12,13,14,15
M(1,1,:,2) = 21,22,23,24,25
M(1,1,:,3) = 31,32,33,34,35
The 1st dimension of C has been correctly converted to the 3rd dimension of M.
In case my simple example is too simple for you, here is the complete 35x10 cell array:
>> C = arrayfun(@(n)rand(256,256),nan(35,10),'uni',0);
>> M = reshape(cat(3,C{:}), [256,256,10,35]); % your buggy answer
>> isequal(M(:,:,10,1),C{1,10}) % your buggy answer
ans = 0
>> M = cell2mat(permute(C,[4,3,2,1])); % my correct answer
>> isequal(M(:,:,10,1),C{1,10}) % my correct answer
ans = 1
The onus of proof lay with me: for that I have provided two examples of why your answer is buggy, I have also explained why it is buggy, and also explained how it can be fixed.
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]))
  8 Comments
Stephen23
Stephen23 on 17 Jun 2023
@debojit sharma: ask a new question. Give sufficient information that we can replicate the error.
debojit sharma
debojit sharma on 17 Jun 2023
@Stephen23 sir, I am trying to implement the code to train VAE for image generation given in the following link using by own dataset of RGB images of size 200*200.
I am getting the following errors in the Train model part:
The code of VAE in the above link is using MNIST dataset images as input to encoder of VAE and it is being said that the decoder of VAE will output an image of size 28-by-28-by-1. But I am trying to generate RGB image of size 200*200 by training this VAE model given in the link. So, my input image is a RGB image of size 200*200. I am getting the above mentioned error in the train model part. I am not able to resolve these errors. So, please kindly guide me @Stephen23 sir regarding what changes I will have to make in this code so that I can train these VAE model to generate RGB image of size 200*200. I will be thankful to you.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!