How to concatenate a 3D cell array along the 3rd dimension?

12 views (last 30 days)
Dear experts, I am trying to convert CellArray1 into CellArray2, by concatenating CellArray1 along the third dimension. Any ideas on how to do that efficiently?
CellArray1 =
2×2×3 cell array
val(:,:,1) =
{180×1 double} {180×1 double}
{180×1 double} {180×1 double}
val(:,:,2) =
{180×1 double} {180×1 double}
{180×1 double} {180×1 double}
val(:,:,3) =
{180×1 double} {180×1 double}
{180×1 double} {180×1 double}
CellArray2 =
2×2 cell array
{540×1 double} {540×1 double}
{540×1 double} {540×1 double}
Thank you so much!

Accepted Answer

Stephen23
Stephen23 on 23 Feb 2020
Edited: Stephen23 on 23 Feb 2020
In one line using num2cell, cellfun, and vertcat:
>> C1 = cell(2,2,3); % preallocate cell array.
>> C1(:) = cellfun(@(~)rand(180,1),C1,'uni',0); % fake data.
>> C2 = cellfun(@(c)vertcat(c{:}),num2cell(C1,3),'uni',0);
And checking the first element of C2:
>> size(C2)
ans =
2 2
>> size(C2{1})
ans =
540 1
>> isequal(C2{1},vertcat(C1{1,1,:}))
ans = 1

More Answers (0)

Categories

Find more on Cell 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!