Arrays of structures indexing

2 ビュー (過去 30 日間)
Luca
Luca 2012 年 5 月 28 日
Sorry for posting. Google is not helping (me) this time. I have a huge array of structures in the spirit of:
a(1).b=[1 2;3 4];
a(1).c=[10 20;30 40]
a(2).b=[5 6;7 8];
a(2).c=[50 60; 70 80]
(to contextualize: b and c are "some properties" of element 1)
Problem: I need to find "3" (in this case a(1).b(2,1) and get (separately):
- [4] ( which is a(1).b(2,2) )
- [30 40] (which is a(1).c(2,:) )
Solution attempt:
I was thinking to use "find" as:
ind=arrayfun(@(x)find(x.b==3),a,'uniformoutput',false)
ind =
[2.00] [0x1 double]
Now my problem is: how do I use these indices to return [4] and [30 40] ?
Thanks a lot for any input!
  1 件のコメント
Oleg Komarov
Oleg Komarov 2012 年 5 月 28 日
Can elaborate on the logic why a(1).b(2,1) leads to pick a(1).b(2,2) and a(1).c(2,:)?

サインインしてコメントする。

回答 (1 件)

Geoff
Geoff 2012 年 5 月 28 日
Start by working out which structures had a result:
bValid = ~cellfun( @isempty, ind );
Now, you need to find the row in b. Since you can't get a row/column output from find in an anonymous function, you should either make a subfunction that does this:
[row,~] = find(...)
Or, simply mod the result by the number of rows in your array:
row = cellfun( @(x) mod(x,2), ind(bValid) );
I still don't know the logic for b... Do you choose the second column? Or do you choose the number next to 3? In this case it doesn't matter. I'll choose the second column:
validIdx = find(bValid);
N = numel(validIdx);
bb = nan(N,1);
cc = cell(N,1);
for n = 1:N
idx = validIdx(n);
bb(n) = a(idx).b(rows(n), 2);
cc(n) = a(idx).c(rows(n), :);
end
Something like that, anyway =)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by