1

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
oldbutnew
  • 145
  • 1
  • 13

2 Answers2

4

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!
user1884905
  • 3,137
  • 1
  • 22
  • 22
  • better yet, is there a way that it will only show that it is match for one person and not mention the rest. – oldbutnew Dec 11 '12 at 18:10
  • @oldbutnew What do you want? Do you want it to only give you the first match? – user1884905 Dec 11 '12 at 19:17
  • yes, that is what i'm trying to do. When I get the real numbers only one of the arrays or none in **person** should match the **person1** array. So if none match I just want it to say "No match found!" once. I added an else statement to say "No match found!" but it says it everytime there is no match. – oldbutnew Dec 11 '12 at 19:39
  • @oldbutnew Ok, if you want to take the first match you find, you could add a `break` in the if-statement (after the printout). – user1884905 Dec 11 '12 at 20:18
0

According to what you wrote, my impression is that you want actually the ratio

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

to be close to one for a match. (Since a/b>=0.85 if a/b<=1 and b/a>=0.85 if b/a<=1 that is 0.85<=a/b<=1/0.85)

You can calculate that like this:

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
Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91
  • Unfortunately this code does not work if some values are smaller and other bigger. For example `person1 = [80 120 80 120 80]` should only be a 81.63% match to `person = [100 100 100 100 100]`, while this code gives a perfect match with a ratio of 1.0. – user1884905 Dec 11 '12 at 12:58