Accessing data from nested cell array

조회 수: 38 (최근 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 CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by