0

I posted a question concerning comparing arrays. I got help with that and it goes something like this:

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!

Now I want to do an error check so if more than one name will be printed the second column in person and person1 will be compared by dividing them by each other. And if the quotient comes out to be at least 0.98, then that name gets printed and only that name. This is what I tried and the error check is not being recognized.

person1(count,:)=[pace height build stride]
allNames = {'Kassie'; 'Keyton'; 'Cameron'; 'Joseph'; 'Josh'};
for z = 1:5
    a = max(person(z,:),person1);
    b = min(person(z,:),person1);
    percent_error = b/a;
    error_count = 0;
    if percent_error >= 0.85
        %title(['Match, its ', allNames{z} ,'!'],...
        %    'Position',[50,20,9],'FontSize',12);
        disp(['Match, its ', allNames{z} ,'!'])
        error_count = error_count+1;
        if error_count >= 2
           ah=max(person(:,2),person1(1,2));
           bh=min(person(:,2),person1(1,2));
           height_check=b/a;
           if height_check >= 0.98
               disp(['Match, its ', allNames{z} ,'!'])
               break
           end
       end
    elseif percent_error < 0.85
        disp('Person is unknown!')
    end
end

Here are the results:

person1 =

    75   168     6    69

Person is unknown!
Match, its Keyton!
Person is unknown!
Match, its Joseph!
Person is unknown!
>> person

person =

    38   163    36    38
    70   162    35    73
    47   166    39    28
    70   163    39    62
    27   176    32    27

all of person1 should be "Person is unknown!" because the values in the 2nd column for Keyton and Joesph are both less than 0.98.

6
  • I read your previous question and in regards to the line a = max(person(z,:),person1); you wrote that you want to "...find out which array is bigger than the other, then I divide the smaller by the larger." If you would clarify in what sense you want to find the larger of these two vectors, then you'll find your answers more informative.( e.g. Do you want to compare the sum of the vectors? Do you want to know which vector contains the largest element?) Commented Dec 13, 2012 at 19:59
  • basically I'm comparing the vectors to each other. but since they won't be the same value I am dividing the smallest by the largest and trying to find out if they are at least 85%. (if they were the small vector it would be 1.0 or 100%. Commented Dec 13, 2012 at 20:06
  • what do you mean by the smallest vector? The line a = max(person(z,:),person1); is an element-wise comparison. That means it compares person(z,ii) to person1(ii) and puts the bigger of the two into a(ii), for all ii. Commented Dec 13, 2012 at 20:12
  • I think that bit is all right, he wants to have ratio<1 so that's easier to compare. There are other ways for doing this, but I don't see any major flaws doing it this way.. Commented Dec 13, 2012 at 20:19
  • @GuntherStruyf thanks for explaining that, I didn't know how else to explain it. What other way would you go about doing this Gunther? Commented Dec 13, 2012 at 20:23

2 Answers 2

4

Do you really want to do a/b? That's the solution in the least squares sense to the underdetermined system of equations a*X = b. See mrdivide and mldivide for more details on this.

Don't you rather want max(a./b) which calculates the ratio of the corresponding elements in a and b and takes the maximum of that. (same thing in the error check)

Btw, your error check isn't run, because you reset error_count in the beginning of the first loop. Move it out of that loop and that's also fixed.

Sign up to request clarification or add additional context in comments.

Comments

1

Let's focus on these few lines:

a = max(person(z,:),person1);
b = min(person(z,:),person1);
percent_error = b/a;
if percent_error >= 0.85

a is going to return the higher of the two values, in the form of an array. b will do the same thing, but the lower. Thus, you have something like this:

a=[98 208 35 114]; b=[93 206 34 107];

The next step you do is a division. However, this is actually a matrix division. In this case, percent_error=0.97408 But I'm not sure why you want to do a least squared sense division, as Guther pointed out. It seems more likely that you want to do something like mean(a./b), to average out the values.\

A few other things that I see:

  • height_check=b/a; Seems like it should be related to bh./ah
  • I'm really unsure about your methods of comparison as a whole. It seems like you would do better to compare each value vs the range of possible values, instead of the division operation you have.

I'm not sure exactly what you are trying to do, but I think you want to do something more like this, with some work on your assumptions required as well:

person1(count,:)=[pace height build stride]
allNames = {'Kassie'; 'Keyton'; 'Cameron'; 'Joseph'; 'Josh'};
for z = 1:5
    a = max(person(z,:),person1);
    b = min(person(z,:),person1);
    percent_error = mean(b./a);
    error_count = 0;
    if percent_error >= 0.85
        %title(['Match, its ', allNames{z} ,'!'],...
        %    'Position',[50,20,9],'FontSize',12);
        disp(['Match, its ', allNames{z} ,'!'])
        error_count = error_count+1;
        if error_count >= 2
           ah=max(person(:,2),person1(1,2));
           bh=min(person(:,2),person1(1,2));
           height_check=bh./ah;
           if height_check >= 0.98
               disp(['Match, its ', allNames{z} ,'!'])
               break
           end
       end
    elseif percent_error < 0.85
        disp('Person is unknown!')
    end
end

1 Comment

I wanted to compare each element and make sure they were close enough, but I'm not that good at writing code so I figured this would get the job done. thanks for pointing out the mistake in height_check. I want to check each element and if 3 out of 4 are around 85-90% then I want it do be a match. I don't know how to do that though, so that is why I didn't try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.