Accessing data from nested cell array

41 Ansichten (letzte 30 Tage)
Durga Lal Shrestha
Durga Lal Shrestha am 23 Nov. 2016
Kommentiert: Sterling Baird am 3 Jul. 2020
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?

Akzeptierte Antwort

KSSV
KSSV am 23 Nov. 2016
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 Kommentare
Durga Lal Shrestha
Durga Lal Shrestha am 23 Nov. 2016
Thanks KSSV.
Sterling Baird
Sterling Baird am 3 Jul. 2020
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by