MATLAB: Bulk concatenation of cell array contents (without looping)

arraybulkcellconcatenateelementlooploopingMATLAB

Hi,
I have a 1×4 cell whose elements are all 1×40 cells. Each of the elements of the 1×40 cells is a 1000×128 array of doubles.
Is there a way to concatenate the contents of the elements in the 1×40 cells into a 40000×128 array of doubles without looping through the elements individually?
Thanks, Alex

Best Answer

There may be a way to do it with no loops, but I think this is fast anyway. Here is a full example. Just change the numbers according to your needs....
% Make a nested cell array, as you describe it.
for ii = 1:4 % A 1-by-4 cell
for jj = 1:3 % Each element is a 1-by-3 cell.
BC{ii}{jj} = rand(2,5); %#ok And each has is a 2-by-5 double
end
end
%



%
%
%
% Now extract it.
A = zeros(2*3,5,4);
for ii = 1:4
A(:,:,ii) = cat(1,BC{ii}{:});
end
A = reshape(permute(A,[1,3,2]),2*3*4,5);