MATLAB:比较阵列到阵列的矩阵 [英] MATLAB: comparing an array to a matrix of arrays

查看:130
本文介绍了MATLAB:比较阵列到阵列的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵,其中数字的每一行重新present值一个人....
   =

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

(我这里的数字是不是真的,我有数字)
我想比较阵列的 PERSON1 = [93 208 34 107]与人的每一行即可。我找出阵列比其它更大,那么我由较大除以越小。如果商大于或等于0.85则存在着匹配和人的名字将打印到屏幕上。我应该使用一个循环和几个if / else语句就像我下面?我敢肯定有是这样做的更好的方法。

 用于z = 1:5
    若Z == 1
        a =最长(人(Z,:),PERSON1);
        B =分钟(人(Z,:),PERSON1);
        percent_error = B / A;
        如果percent_error> = 0.85
            标题(匹配,其卡梅伦!','位置',[50,20,9],字号,12);
        结束
    ELSEIFž== 2
        a =最长(人(Z,:),PERSON1);
        B =分钟(人(Z,:),PERSON1);
        percent_error = B / A;
        如果percent_error> = 0.85
            标题(匹配,它的大卫!','位置',[50,20,9],字号,12);
        结束
    ELSEIFž== 3
        a =最长(人(Z,:),PERSON1);
        B =分钟(人(Z,:),PERSON1);
        percent_error = B / A;
        如果percent_error> = 0.85
            标题(匹配,它的迈克!','位置',[50,20,9],字号,12);
        结束
        。
        。
        。
        等等...
    结束
结束


解决方案

对于初学者来说,你可以得到通过存储在单元格中的所有名称摆脱所有的if语句的。

  allNames = {'卡梅隆的; 大卫; '麦克风'; '法案'; '乔'};

下面是如何那么会得到他们抓住在一个循环:

 人= [98 206 35 114;
          60 206 28 52;
         100 210 116 31;
          69 217 26 35;
          88 213 42 100];PERSON1 = [93 208 34 107];allNames = {'卡梅隆的; 大卫; '麦克风'; '法案'; '乔'};用于z = 1:5
    a =最长(人(Z,:),PERSON1);
    B =分钟(人(Z,:),PERSON1);
    percent_error = B / A;
    如果percent_error> = 0.85
        %标题(['匹配,它的',allNames {Z},'!'],...
        %'位置',[50,20,9],字号,12);
        DISP(['匹配,它的',allNames {Z}'!'])
    结束
结束

运行code它会显示:

 匹配,其卡梅隆!
全场比赛,它的大卫!
全场比赛,它的迈克!
全场比赛,其乔!

I have a matrix where each row of numbers represent values for a person.... person =

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

(The numbers I have here aren't really the numbers that I have) I want to compare array person1 = [93 208 34 107] with each row of the person. I find out which array is bigger than the other, then I divide the smaller by the larger. If the quotient is greater than or equal to 0.85 then there is a match and the name of the person will print to the screen. Should I use a loop and several if/else statements like what I have below? I'm sure there is a better method to doing this.

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

解决方案

For starters, you can get rid of all the if-statements by storing all the names in a cell.

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

Here is how you then would get hold of them in a loop:

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

Running the code it will display:

Match, its Cameron!
Match, its David!
Match, its Mike!
Match, its Joe!

这篇关于MATLAB:比较阵列到阵列的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆