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

findsimple

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!

Best Answer

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.