I am currently attempting to use the find function in Matlab and can not get it to work. Help?

5 views (last 30 days)
vec = [0:.5:10];
cont = 1;
store = 0;
while( cont ==1)
try
score = input('Please enter a test score: ');
disp(score == .5000)
if(find(vec,score))
disp(score)
end
catch
disp('Please enter scores in increments of .5')
end
cont = input('Please enter 1 to continue: ');
end
This is the code I am currently working with. I am trying build a test averaging program for a professor and he assured me that all test scores would be in increments of .5 ; I am using the find function to confirm that they are increments of .5 and that the test scores are within the valid range. For some reason I can only get true within my if statement when I have score of increments of 1 and not .5 ; does anyone have a solution to this or an explanation? Thanks in advance!

Accepted Answer

dpb
dpb on 10 Feb 2016
You're testing only for the one case in which the score is precisely 0.5, for the disp statement not that the score is a multiple of 0.5.
The syntax is wrong for find, the second (optional) argument is the number of elements to find and must be an integer, not the value to match. find evaluates a logical condition so the correct syntax to find the element in the lookup array would be
if find(vec==score)
That is, however, a somewhat wasteful approach, consider what
mod(score*10,5)
returns for various scores and what test would satisfy the condition desired? Then you can simply ensure that the score is also >0 and <= whatever the maximum allowable would be.
  1 Comment
mwienands
mwienands on 10 Feb 2016
Thanks for the help! This is exactly what I was hoping to find. I will defiantly take the second approach into account when going forward with this. Thanks for the clarification on the find function!

Sign in to comment.

More Answers (1)

Adam
Adam on 10 Feb 2016
Edited: Adam on 10 Feb 2016
Floating point numbers are always very risky to use in absolute equality statements due to the way they are represented.
You should always test within a tolerance for these to be sure.
score == .5000
should be replaced by something more like
tolerance = 1e-6;
abs( score - 0.5 ) < tolerance;
though you have to determine that '1e-6' value, the tolerance, yourself. It depends on the situation, sometimes it can be really low, even using the builtin eps function, sometimes it needs to be higher.
Also sometimes it is better to use a relative tolerance than an absolute tolerance. e.g. if your numbers are all of the order of 10000000 then using an absolute tolerance of 1e-20 does not make sense, whereas if your numbers are all of the order of 0.00001 then clearly your tolerance needs to be lower than the 1e-6 I used.
In your case an absolute tolerance should be fine since your numbers are of the order of 1-10/.
  1 Comment
dpb
dpb on 10 Feb 2016
While true in general, 0.5 is exactly representable so OP is unlikely to have an issue here where the input will (presumably) be either manually entered or read from a transcript or the like, not computed and so things that would computed be close to 0.5 but not exact aren't going to occur. But, again, it never hurts to make a newbie aware...
K>> sind(30)==0.5
ans =
0
>> sind(30)-0.5
ans =
-5.5511e-17
>>

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!