MATLAB: How to convert a 3d matrix to a matrix of cells

cell arraysmatrixmatrix to cell array

I have a 3d matrix M of size 3*4*3
M(:,:,1) = [1.2 3 5 6;
5 21 8 -3;
4 1 4 9]
M(:,:,2) = [7 3.4 7 33;
-5 7 7 23;
0 -23 8 0]
M(:,:,3) = [64 0 0 7;
7 0 8 -3;
0 0 3 0]
Now I want to have a 2d matrix of cells C with a size 3*4 and cells being like
C{1,1} = [1.2; 7; 64]
.
.
C{2,1} = [5; -5; 7]
.
.
C{3,4} = [9; 23; 0]
Can I do this in one shot without loops and also is it possible to get back M using this C?
Thanks for your help in advance 🙂

Best Answer

C = cellfun(@(x)reshape(x,[],1),num2cell(M,3),'un',0);