Accessing data from nested cell array

50 ビュー (過去 30 日間)
Durga Lal Shrestha
Durga Lal Shrestha 2016 年 11 月 23 日
コメント済み: Sterling Baird 2020 年 7 月 3 日
I have the following nested cell array
values{1,1}{1,1} = '1_1';
values{1,1}{1,2} = '1_2';
values{1,2}{1,1} = '2_1';
values{1,2}{1,2} = '2_2';
values{1,3}{1,1} = '3_1';
values{1,3}{1,2} = '3_2';
I would like to access data as shown below:
for i=1:length(values)
firstValues{i} = values{1,i}{1,1};
secondValues{i} = values{1,i}{1,2};
end
How can we access data without loop?

採用された回答

KSSV
KSSV 2016 年 11 月 23 日
values{1,1}{1,1} = '1_1';
values{1,1}{1,2} = '1_2';
values{1,2}{1,1} = '2_1';
values{1,2}{1,2} = '2_2';
values{1,3}{1,1} = '3_1';
values{1,3}{1,2} = '3_2';
firstvalues = cellfun(@(x) x{1}(:)', values, 'UniformOutput', false)
secondvalues = cellfun(@(x) x{2}(:)', values, 'UniformOutput', false)
  2 件のコメント
Durga Lal Shrestha
Durga Lal Shrestha 2016 年 11 月 23 日
Thanks KSSV.
Sterling Baird
Sterling Baird 2020 年 7 月 3 日
I suppose technically it's not a loop, but it should be noted that cellfun() and arrayfun() are typically somewhat slower than for loops. See https://www.mathworks.com/matlabcentral/answers/42335-array-cellfun-vs-for-loop

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by