# Writing a find function

3 views (last 30 days)
Nicholas Deosaran on 13 Oct 2020
Commented: Stephen23 on 13 Oct 2020
Hello all,
I am trying to run a function that will use the "find" function to separate grades
i have my code below, but I dont know what I seem to be doing wrong.
scores = 100*rand(50,1);
ind1 = find(scores >= 90); %A
ind2 = find(scores <= 80) && ( scores < 90 ); % B
ind3 = find(scores <=70) && (scores < 80); % C
ind4 = find(scores <= 60) && ( scores <70); % D
ind5 = find (scores <60); % f
vec_new(ind1) = 'A';
vec_new(ind2) = 'B' ;
vec_new(ind3) = 'C' ;
vec_new(ind4) = 'D' ;
vec_new(ind5) = 'F';
out_orig = scores;
out_new = vec_new ;
end
the error I am getting is
Operands to the || and && operators must be convertible to logical scalar values.
ind2 = find(scores <= 80) && ( scores < 90 ); % B
Nicholas Deosaran on 13 Oct 2020
Hey, thank you for that.
The problem I was having which I should of stated above is:
Operands to the || and && operators must be convertible to logical scalar values.
ind2 = (scores <= 80) && ( scores < 90 ); % B

KSSV on 13 Oct 2020
Edited: KSSV on 13 Oct 2020
You can proceed something like htis:
score = randperm(100,1)
if score >=90
elseif (score >= 80) & (score < 90 )
elseif (score >= 70) & (score < 80)
elseif (score >= 60) & (score < 70 )
else
end
If you want score to be a vector, proceed like this:
score = randperm(100,10) ;
idx = zeros(size(score)) ;
idx(score >= 90) = 1 ;
idx(score >= 80 & score < 90) = 2 ;
idx(score >= 70 & score < 80) = 3 ;
idx(score >= 60 & score < 70) = 4 ;
idx(idx==0) = 5 ;
grade = {'A', 'B', 'C', 'D', 'E'} ;
Stephen23 on 13 Oct 2020
"Can it be used it find?"
Using find is less efficient than the simple logical indexing that KSSV's answer shows.