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

Jan
Jan 2014년 4월 4일
It depends on how "parent value" and "its children" is defined. Are you looking for the subtree index of e.g. [2,2,2] or do you want to obtain all column values of the Children fields for all Parent values like 2? Please post a meaningful example.
Sososasa
Sososasa 2014년 4월 4일
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]

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

 채택된 답변

Jos (10584)
Jos (10584) 2014년 4월 4일

1 개 추천

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

Sososasa
Sososasa 2014년 4월 5일
Oh thats really great answer. it turns out I have 2D structure subtree(i,j).Parent; subtree(i,j).Children How can I get i and j for the subtree if I know a Parent value ?
You can convert linear indices to subindices using IND2SUB
[i,j] = ind2sub(size(subtree),IDX)

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

추가 답변 (1개)

Chandrasekhar
Chandrasekhar 2014년 4월 4일

1 개 추천

Struct Find I hope this will help you.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 4월 4일

댓글:

2014년 4월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by