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

13 次查看(过去 30 天)
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!

采纳的回答

Stephen23
Stephen23 2020-2-23
编辑:Stephen23 2020-2-23
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by