1

数値の各行が人の値を表す行列があります。... person =

 98   206    35   114
 60   206    28    52
100   210    31   116
 69   217    26    35
 88   213    42   100

(ここにある数字は、実際には私が持っている数字ではありません)配列person1 = [93 20834107]をpersonの各行と比較したいと思います。どの配列が他の配列よりも大きいかを見つけて、小さい方の配列を大きい方の配列で割ります。商が0.85以上の場合、一致があり、人物の名前が画面に印刷されます。ループと、以下のようないくつかのif / elseステートメントを使用する必要がありますか?これを行うにはもっと良い方法があると確信しています。

for z = 1:5
    if z == 1
        a = max(person(z,:),person1);
        b = min(person(z,:),person1);
        percent_error = b/a;
        if percent_error >= 0.85
            title('Match,its Cameron!','Position',[50,20,9],'FontSize',12);
        end
    elseif z ==2
        a = max(person(z,:),person1);
        b = min(person(z,:),person1);
        percent_error = b/a;
        if percent_error >= 0.85
            title('Match,its David!','Position',[50,20,9],'FontSize',12);
        end
    elseif z == 3
        a = max(person(z,:),person1);
        b = min(person(z,:),person1);
        percent_error = b/a;
        if percent_error >= 0.85
            title('Match,its Mike!','Position',[50,20,9],'FontSize',12);
        end
        .
        .
        .
        so on...
    end
end
4

2 に答える 2

4

手始めに、セルにすべての名前を格納することで、すべてのifステートメントを取り除くことができます。

allNames = {'Cameron'; 'David'; 'Mike'; 'Bill'; 'Joe'};

次に、ループでそれらを取得する方法を示します。

person = [98   206    35   114;
          60   206    28    52;
         100   210    31   116;
          69   217    26    35;
          88   213    42   100];

person1 = [93 208 34 107];

allNames = {'Cameron'; 'David'; 'Mike'; 'Bill'; 'Joe'};

for z = 1:5
    a = max(person(z,:),person1);
    b = min(person(z,:),person1);
    percent_error = b/a;
    if percent_error >= 0.85
        %title(['Match, its ', allNames{z} ,'!'],...
        %    'Position',[50,20,9],'FontSize',12);
        disp(['Match, its ', allNames{z} ,'!'])
    end
end

コードを実行すると、次のように表示されます。

Match, its Cameron!
Match, its David!
Match, its Mike!
Match, its Joe!
于 2012-12-11T08:20:05.400 に答える
0

あなたが書いたものによると、私の印象はあなたが実際に比率を望んでいるということです

a=person(i,:)
b=person1  % i=1..5
a/b

試合のために1に近くなります。(a / b<=1の場合はa/b> = 0.85、b /a<=1の場合はb/a> = 0.85、つまり0.85 <= a / b <= 1 / 0.85であるため)

あなたはこのようにそれを計算することができます:

ratio = person/person1;
idx = 1:5;
idx_found = idx(ratio>=0.85 & ratio<1/0.85);

for z=idx_found
    disp(['Match, its ', allNames{z} ,'!'])
end
于 2012-12-11T11:50:01.940 に答える