Composing arrayfun syntax to extract data from structure

Here is a simplified example of structure:
C(1,1).idx = [1;1;2;3];
C(2,1).idx = [1;1;2];
C(1,1).s = {'a';'a';'ab';'abc'};
C(2,1).s = {'a';'a';'ab'};
now, find unique idx in structure C:
[ua,ia,~] = arrayfun(@(x) unique(x.idx, 'rows'), C, 'UniformOutput', false);
How do I apply indexes ia to extract corresponding values in s fields? I can think of for loop but is there some arrayfun trick? Side note, in my real data idx is nx4 matrix therefore 'rows' argument.

댓글 수: 5

It's not clear for me
Matt Fig
Matt Fig 2012년 11월 13일
편집: Matt Fig 2012년 11월 13일
You realize that ARRAYFUN is almost always slower than a FOR loop, right? I just want to make sure you are not going this route to gain speed...
+1 to Matt Fig's comment.
@Matt & @ Sean
I realize ARRAYFUN is slower in many cases, just trying to warp my head around ARRAYFUN and its use.
@Azzi
In my example:
ia{1,1} = [2;3;4];
ia{2,1} = [2;3];
Now, how would you, using indexes ia and ARRAYFUN, extract corresponding values from field s?

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt Fig
Matt Fig 2012년 11월 13일
편집: Matt Fig 2012년 11월 13일
O.k., if you are just messing around with ARRAYFUN.
C(1,1).idx = [1;1;2;3];
C(2,1).idx = [1;1;2];
C(1,1).s = {'a';'a';'ab';'abc'};
C(2,1).s = {'a';'a';'ab'};
[ua,ia,~] = arrayfun(@(x) unique(x.idx,'rows'),C,'Un',0);
V = arrayfun(@(x) C(x).s(ia{x}),1:length(C),'Un',0)

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 11월 13일
arrayfun(@(rowidx,idx) C(rowidx).s(idx{1}), 1:size(C,1), ia, 'UniformOutput', false)
or perhaps this might work:
arrayfun( @(idx, s) s{idx{1}}, ia, {C.s}, 'UniformOutput', false)

카테고리

도움말 센터File Exchange에서 Time Series Objects에 대해 자세히 알아보기

제품

질문:

2012년 11월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by