Bulk concatenation of cell array contents (without looping)?
6 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a 1x4 cell whose elements are all 1x40 cells. Each of the elements of the 1x40 cells is a 1000x128 array of doubles.
Is there a way to concatenate the contents of the elements in the 1x40 cells into a 40000x128 array of doubles without looping through the elements individually?
Thanks, Alex
0 个评论
采纳的回答
Matt Fig
2011-4-23
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);
更多回答(2 个)
Ramis Rafay
2017-7-23
I think this will work for any given number of elements without looping
stackedarrays = vertcat(mycell{:,:})
1 个评论
theblueeyeswhitedragon
2018-6-27
编辑:theblueeyeswhitedragon
2018-6-27
This gives you back a mxn matrix, which you could easily obtain by using cell2mat().
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!