How to combine cell arrays to form one nested cell array entry

3 ビュー (過去 30 日間)
Alyssa
Alyssa 2024 年 5 月 21 日
編集済み: Voss 2024 年 5 月 21 日
Hello, I have a variable (X) that is a cell array (size 64X634). In the each location of cell array X, there is a nested 1x2 cell array.
How can I combinethe nested 1x2 cell arrays across the 634 columns in X such that the variable(X) is now the desired size of 64x1, where each row entry of the cell arrray X contains the new 634x2 nested cell array?
In other words, I want to combine each of the 1x2 cell arrays found in the columns of the original variable(X) so that each row of variable(X) only has one column (now a nested cell array with all the original 1x2 nested cell arrays). Thanks!

採用された回答

Voss
Voss 2024 年 5 月 21 日
% 4x3 instead of 64x634, for demonstration
X = { ...
{1 2} {3 4} {5 6}; ...
{7 8} {9 10} {11 12}; ...
{13 14} {15 16} {17 18}; ...
{19 20} {21 22} {23 24}; ...
};
X
X = 4x3 cell array
{1x2 cell} {1x2 cell} {1x2 cell} {1x2 cell} {1x2 cell} {1x2 cell} {1x2 cell} {1x2 cell} {1x2 cell} {1x2 cell} {1x2 cell} {1x2 cell}
X{1,1},X{1,2},X{1,3}
ans = 1x2 cell array
{[1]} {[2]}
ans = 1x2 cell array
{[3]} {[4]}
ans = 1x2 cell array
{[5]} {[6]}
N = size(X,1);
Y = cell(N,1);
for ii = 1:N
Y{ii} = vertcat(X{ii,:});
end
X = Y;
X
X = 4x1 cell array
{3x2 cell} {3x2 cell} {3x2 cell} {3x2 cell}
X{1}
ans = 3x2 cell array
{[1]} {[2]} {[3]} {[4]} {[5]} {[6]}
  2 件のコメント
Alyssa
Alyssa 2024 年 5 月 21 日
Thank you! I had tried concat at first and it didn't work, vertcat works!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by