MATLAB: Composing arrayfun syntax to extract data from structure

arrayfunindexingMATLAB

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.

Best Answer

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)