How to unnest cell arrays using a for loop?

A = [{{{{4}}}}, {0}, {{1}}, {{{0}}}]
I need to unnest the cell array so I am left with just the numbers in a vector. [4 0 1 0]
I think you can use a for loop..
How do I do this?

 採用された回答

Jan
Jan 2019 年 3 月 10 日
編集済み: Jan 2019 年 3 月 11 日

3 投票

A = [{{{{4}}}}, {0}, {{1}}, {{{0}}}]
B = zeros(size(A));
for iA = 1:numel(A)
a = A{iA};
while iscell(a)
a = a{1};
end
B(iA) = a;
end
Or:
c = true;
while any(c)
c = cellfun('isclass', A, 'cell');
A(c) = cellfun(@(x) x, A(c));
end
B = [A{:}]

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

質問済み:

2019 年 3 月 10 日

編集済み:

Jan
2019 年 3 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by