私は構造体を持っています。結果は次のようになります:
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}
結果はセル配列の構造体であり、各要素の血縁関係を示します。次の関数を作成しました。これは、それぞれの class_name (me、mom など) を受け取り、それぞれのセルであり、すべての親戚を返します。たとえば、 recur_getchildren(mom,{grandpa,me,sis},result) は、お母さんのすべての血縁者と、それらの親戚のすべての親戚を返す必要があります(誰もいなくなるまで、同様に続きます):
ans=half_sis、私、お父さん、おじいちゃん、
お母さんは私、お父さん、おじいちゃんと血縁関係はありませんが、それでもリストに表示されます。上記の例は簡略化したものです。私の問題では、さらに多くの世代を追跡する必要があります。私は次のことを試みましたが、正確には機能していません。私は何を間違っていますか?これを行う簡単な方法はありますか?
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,'')) && 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