Accessing data from nested cell array

51 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

KSSV
KSSV il 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 Commenti
Sterling Baird
Sterling Baird il 3 Lug 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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by