1

私は構造体を持っています。結果は次のようになります:

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
4

1 に答える 1

0

これは、文字列の評価と設定操作を使用して実行できると思います。

私たちが持っている場合

result = 

      me: {'dad'  'grandpa'  'half_sis'}
      step_mom: {'half_sis'}
     dad: {'me'  'grandpa'  'half_sis'}
half_sis: {'mom'  'me'  'dad'  'grandpa'}
 grandpa: {'me'  'dad'  'half_sis'}

1人を選択して、その人の親戚を見つけることができます。

s = result.step_mom

s = 

    'half_sis'

いくつかの文字列操作を使用して、次のような親戚の親戚を見つけることができます。

eval(char(strcat('result.',s)))

ans = 

    'mom'    'me'    'dad'    'grandpa'

これにより、関数のアイデアが得られます。

function children=recur_getchildren(result,person)

%Find all relatives of given person
s = eval(strcat('result.',person));

%Go through all relatives found in s
for i=1:size(s,2)
    children = union(s(i),eval(char(strcat('result.',s(i)))));
end

%remove orignal relative from other relatives
children=setdiff(children,person);

それをテストしましょう:

relatives=recur_getchildren(result,'mom')

relatives = 

    'dad'    'grandpa'    'half_sis'    'me'

これをすべての場合に機能させるには、step_momをmomに(またはその逆に)変更する必要があることに注意してください。

編集:再帰的にするだけです。このようなもの:

function [All_relatives,not_examined]=...
recur_getchildren(result,person,examined,All_relatives)


%Find all relatives of given person
s = eval(strcat('result.',person));

children = '';

%Go through all relatives found in s
for i=1:size(s,2)
    children = union(children,eval(char(strcat('result.',s(i)))));
end

%Save all found relatives
All_relatives = union(All_relatives,children);
All_relatives = union(All_relatives,s);

%Save all examined persons
examined = union(examined,s);
examined = union(examined,person);
not_examined = setdiff(All_relatives,examined);


count=0;
while length(not_examined)>0
    count=count+1;
    examined = union(examined,not_examined(count ));
    [new_relatives,not_examined] = recur_getchildren(result,char(not_examined(count)),examined,All_relatives);
    All_relatives = union(All_relatives,new_relatives);

end

これはすぐにまとめられましたが、機能するはずです。実行時に、調べて* All_relatives *入力を空の文字列として指定します。

children=recur_getchildren(result,'me','','')
于 2012-07-24T17:51:05.713 に答える