Bulk concatenation of cell array contents (without looping)?

6 ビュー (過去 30 日間)
Alex
Alex 2011 年 4 月 23 日
編集済み: theblueeyeswhitedragon 2018 年 6 月 27 日
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

採用された回答

Matt Fig
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);
  1 件のコメント
Danny Gonzalez
Danny Gonzalez 2013 年 10 月 9 日
This really helped out, thanks!

サインインしてコメントする。

その他の回答 (2 件)

Alex
Alex 2011 年 4 月 23 日
Ooops, I figured it out:
stackedarrays = vertcat(bigcell{1,1}{1,:})
If there is a better way, please let me know.
  1 件のコメント
Ali Ali
Ali Ali 2011 年 9 月 19 日
Thanks a lot for your solution

サインインしてコメントする。


Ramis Rafay
Ramis Rafay 2017 年 7 月 23 日
I think this will work for any given number of elements without looping
stackedarrays = vertcat(mycell{:,:})
  1 件のコメント
theblueeyeswhitedragon
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 ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by