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.
function [out_orig, out_new] = parse_grades
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.
Error in parse_grades (line 6)
ind2 = find(scores <= 80) && ( scores < 90 ); % B

댓글 수: 4

Ameer Hamza
Ameer Hamza 2020년 10월 13일
You haven't defined A, B, C, D, and F inside the function.
Nicholas Deosaran
Nicholas Deosaran 2020년 10월 13일
Okay, how would I fix that?
Ruger28
Ruger28 2020년 10월 13일
편집: Ruger28 2020년 10월 13일
I think you meant to say:
vec_new(ind1) = 'A';
vec_new(ind2) = 'B';
vec_new(ind3) = 'C';
vec_new(ind4) = 'D';
vec_new(ind5) = 'F';
not saying this will solve your problems, though.
Nicholas Deosaran
Nicholas Deosaran 2020년 10월 13일
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.
Error in parse_grades (line 6)
ind2 = (scores <= 80) && ( scores < 90 ); % B

댓글을 달려면 로그인하십시오.

답변 (1개)

KSSV
KSSV 2020년 10월 13일
편집: KSSV 2020년 10월 13일

0 개 추천

You can proceed something like htis:
score = randperm(100,1)
if score >=90
grade = 'A' ;
elseif (score >= 80) & (score < 90 )
grade = 'B' ;
elseif (score >= 70) & (score < 80)
grade = 'C' ;
elseif (score >= 60) & (score < 70 )
grade = 'D' ;
else
grade = 'E' ;
end
grade
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'} ;
iwant = grade(idx)

댓글 수: 6

Nicholas Deosaran
Nicholas Deosaran 2020년 10월 13일
Yes, but I will like to use it with the "Find" function if possible?
KSSV
KSSV 2020년 10월 13일
I have edited the answer...with logical indices..plese check.
Nicholas Deosaran
Nicholas Deosaran 2020년 10월 13일
Can it be used it find?
KSSV
KSSV 2020년 10월 13일
score = randperm(100,10) ;
idx1 = find(score > 90) % A grade
idx2 = find(score >= 60 & score < 70) % C Grade
Nicholas Deosaran
Nicholas Deosaran 2020년 10월 13일
is the idx1 --- > idx= zeros(size(score)); ??
Stephen23
Stephen23 2020년 10월 13일
"Can it be used it find?"
Using find is less efficient than the simple logical indexing that KSSV's answer shows.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2020년 10월 13일

댓글:

2020년 10월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by