Reputation: 145
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
Upvotes: 1
Views: 559
Reputation: 12514
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
Upvotes: 0
Reputation: 3137
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!
Upvotes: 4