Note that there are some explanatory texts on larger screens.

plurals
  1. POrecursion in rearranging related data
    primarykey
    data
    text
    <p>I have a struct, result like so:</p> <blockquote> <pre><code>result.me={dad,grandpa,half_sis} result.step_mom={half_sis} result.dad={me,grandpa,half_sis} result.half_sis={mom,me,dad,grandpa} result.grandpa={me,dad,half_sis} </code></pre> </blockquote> <p>Result is a struct of cell arrays and shows the relatives(by blood) of each element. I wrote the following function, which takes the respective class_name(me,mom, etc) and it's respective cell and returns all of the relatives. For example, recur_getchildren(mom,{grandpa,me,sis},result) should return all the blood relative of mom AND all of those relative's relatives(and so on, until no one is left):</p> <blockquote> <p>ans=half_sis,me,dad,grandpa,</p> </blockquote> <p>so even though mom is not related to me,dad or grandpa, they still get listed. The above example is a simplification, for my problem there will be many many more generations to keep track of. I tried to do the following, but it's not exactly working. What am I doing wrong? Is there an easier way to do this?</p> <pre><code> function tot=recur_getchildren(class_name,each_cell,result) %gets all the children for an element and all its descendants global gresult if(~isfield(gresult,class_name)) gresult.(class_name)=1; w=size(each_cell,2); tot=struct(char(class_name),1); %include the class_name item as well %tot is a struct where all the keys indicate the progeny, and val is 0/1 for m=1:w %the child must not have already been searched previously if(~isfield(gresult,each_cell{m})) gresult.(char(each_cell{m}))=1; tot.(char(each_cell{m}))=1; % copy all contents over to parent if(sum(~strcmp(each_cell,'')) &amp;&amp; sum(isfield(result,char(each_cell{m})))) s=size(result.(char(each_cell{m})),2); %run iteratively over all the children of each member of %each_cell for j=1:s %struct of all childs %gresult.(each_cell{m})=1; if(~isfield(gresult,char(result.(char(each_cell{m})){j}))) tot.(char(result.(char(each_cell{m})){j}))=1; % tot_records.(char(result.(char(each_cell{m})){j}))=1 tot2=recur_getchildren(each_cell{m},result.(char(each_cell{m}))(j),result); %check off this so that we dont search it again gresult.(char(result.(char(each_cell{m})){j}))=1; %keep looping to get all children %loop through result struct if(size(tot2,2)~=0) fn=fieldnames(tot2); for p=1:size(fn) str=fn(p); recur_getchildren(char(str),result.(char(str)),result) gresult.(char(str))=1; end end end end end end end else tot=[]; end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload