Clear Filters
Clear Filters

Concatenate rows within a 3D cell array

1 view (last 30 days)
I originally had a 3D structure with 6 fields.
structure(1:17,1:10,1:16).A
structure(1:17,1:10,1:16).B
structure(1:17,1:10,1:16).C
structure(1:17,1:10,1:16).D
structure(1:17,1:10,1:16).E
structure(1:17,1:10,1:16).F
For each location, I would like to concatente A,C&E so I end up with a 3D (17x10x16) cell array with A,C&E in horizontal cat for each location.
For example, structure(17,10,16).A is a 1 x10 cell array
[2×149 double] [2×146 double] [2×142 double] [2×144 double] [2×147 double] [2×147 double] [2×150 double] [2×145 double] [2×141 double] [2×145 double]
structure(17,10,16).C is a 1 x10 cell array
[2×479 double] [2×482 double] [2×487 double] [2×486 double] [2×475 double] [2×489 double] [2×491 double] [2×482 double] [2×474 double] [2×466 double]
structure(17,10,16).E is a 1 x10 cell array
[2×454 double] [2×461 double] [2×500 double] [2×464 double] [2×463 double] [2×475 double] [2×499 double] [2×468 double] [2×483 double] [2×497 double]
I started by:
cat_1=arrayfun(@(x) [x.A;x.C;x.E],structure,'uni', false)
It gives a 17x10x16 cell array with a {3x10 cell} in each location.
For example , cat_1{17,10,16} is
[2×149 double] [2×146 double] [2×142 double] [2×144 double] [2×147 double] [2×147 double] [2×150 double] [2×145 double] [2×141 double] [2×145 double]
[2×479 double] [2×482 double] [2×487 double] [2×486 double] [2×475 double] [2×489 double] [2×491 double] [2×482 double] [2×474 double] [2×466 double]
[2×454 double] [2×461 double] [2×500 double] [2×464 double] [2×463 double] [2×475 double] [2×499 double] [2×468 double] [2×483 double] [2×497 double]
Now, I am stuck on concatenating each column so I endup with a {1x10 cell} in each locacion.
For example, cat_2{17,10,16} to be
[2x1082 double] [2x1089 double] [2x1129 double] [2x1094 double] [2x1085 double] [2x1111 double] [2x1140 double] [2x1095 double] [2x1098 double] [2x1108 double]
I have tried several combiantions of cellfun and arrayfun with horcat, cat in different dimensions but I do not get the above organization for each location of the 3D cell array.
Thank you,

Accepted Answer

Stephen23
Stephen23 on 14 Nov 2018
Edited: Stephen23 on 14 Nov 2018
Try something like this:
baz = @(c) horzcat(c{:});
foo = @(x) cellfun(baz, num2cell([x.A;x.C;x.E],1), 'uni',0);
arrayfun(foo, structure, 'uni',false)

More Answers (0)

Categories

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