search in data structure type

3 views (last 30 days)
Sososasa
Sososasa on 4 Apr 2014
Commented: Jos (10584) on 6 Apr 2014
Hi, I have a struct type array:
subtree(1).Parent= [2 2 2]
subtree(1).Children= [1 2 3; 3 2 4]
subtree(2).Parent= [1 2 2]
subtree(2).Children= [1 2 3; 5 5 5]
My question is, if I know the Parent value=[2 2 2] and I don't know the subtree index, how can I find/search its children=[1 2 3; 3 2 4]? In another words how I can get the index of the subtree that contain Parent= [2 2 2]
  2 Comments
Sososasa
Sososasa on 4 Apr 2014
Are you looking for the subtree index of e.g. [2,2,2] ? Yes exactly, [2 2 2] is a parent and I want to get the children [1 2 3; 3 2 4]

Sign in to comment.

Accepted Answer

Jos (10584)
Jos (10584) on 4 Apr 2014
TargetParent = [2 2 2]
TF = arrayfun(@(k) isequal(subtree(k).Parent, TargetParent), 1:numel(subtree)) % logical indices
IDX = find(TF) % convert to linear indices
  2 Comments
Jos (10584)
Jos (10584) on 6 Apr 2014
You can convert linear indices to subindices using IND2SUB
[i,j] = ind2sub(size(subtree),IDX)

Sign in to comment.

More Answers (1)

Chandrasekhar
Chandrasekhar on 4 Apr 2014
Struct Find I hope this will help you.

Community Treasure Hunt

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

Start Hunting!