Cell Array Reshape Operation

6 views (last 30 days)
MarshallSc
MarshallSc on 18 Jan 2022
Edited: MarshallSc on 19 Jan 2022
I have this 10x10 cell array:
I want to reshape it into a 4x4 cell each containing a 10x10 matrix keeping the order of each value in the matrices. For example, the first new cell ( cell(1,1) ) contains the first values of the 16x1 matrices which will transform into a 10x10 for each cell and so on. I'd appreciate it if someone can please help me! Thank you!

Accepted Answer

DGM
DGM on 19 Jan 2022
There are probably other ways, but...
% a patterned test array
A = repmat({(1:16).', (17:32).'; (33:48).', (49:64).'},5,5)
A = 10×10 cell array
{16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double} {16×1 double}
B = cell2mat(A(:).');
B = reshape(permute(B,[2 3 1]),10,10,[]);
B = reshape(num2cell(B,[1 2]),4,4)
B = 4×4 cell array
{10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double} {10×10 double}
% inspect the result
B{1}
ans = 10×10
1 17 1 17 1 17 1 17 1 17 33 49 33 49 33 49 33 49 33 49 1 17 1 17 1 17 1 17 1 17 33 49 33 49 33 49 33 49 33 49 1 17 1 17 1 17 1 17 1 17 33 49 33 49 33 49 33 49 33 49 1 17 1 17 1 17 1 17 1 17 33 49 33 49 33 49 33 49 33 49 1 17 1 17 1 17 1 17 1 17 33 49 33 49 33 49 33 49 33 49
B{2}
ans = 10×10
2 18 2 18 2 18 2 18 2 18 34 50 34 50 34 50 34 50 34 50 2 18 2 18 2 18 2 18 2 18 34 50 34 50 34 50 34 50 34 50 2 18 2 18 2 18 2 18 2 18 34 50 34 50 34 50 34 50 34 50 2 18 2 18 2 18 2 18 2 18 34 50 34 50 34 50 34 50 34 50 2 18 2 18 2 18 2 18 2 18 34 50 34 50 34 50 34 50 34 50

More Answers (0)

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!