Accessing data from nested cell array

38 次查看(过去 30 天)
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 个评论
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 CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by