I can't comput the following commands
1 view (last 30 days)
Show older comments
if total>=90 && total<=100
A1=sum(total>=90 && total<=100);
frpintf('The number of A+ students is : %d\n',A1)
elseif total>=80 && total<90
A=sum(total>=80 && total <90);
frpintf('The number of A students is : %d\n',A2)
elseif total>= 70 && total<80
B=sum(total >= 70 && total<80);
fprintf('The number of B students is %d\n',B)
elseif total>=60 && total<70
C=sum(total>=60 && total <70);
fprintf('The number of C students is %d\n',C)
elseif total>=50 && total<60
D=sum(total>=50 && total<60);
fprintf('The number of D students is: %d\n',D)
elseif total>=40 && total<50
E=sum(total>=40 && total<50);
fprintf('The number of E students is: %d\n',E)
elseif total>=0 && total<40
F=sum(total>=0 && total<40);
fprintf('The number of F students is: %d\n',F)
end
Hi I tried to compute this commands to able get the number of student in each letter grade and I kept getting this error
"Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values. Use the ANY or ALL functions to reduce operands to logical scalar values.
Error in a3q5 (line 41)
elseif total>=80 && total<90"
Can someone please tell what's wrong with my commands, I try to fix but it does not seem really work
0 Comments
Answers (2)
John D'Errico
on 23 Feb 2024
Edited: John D'Errico
on 23 Feb 2024
Sure you "can". It is just that MATLAB fails due to an error. :)
Anyway, you seem to think an if statement applies to each element of a vector, operating separately, doing something different for each element in that vector. It does not. An if statement is not a loop, though this is a common mistake made by new users.
If you are allowed to use it, I might recommend the function discretize to determine which interval each element of the total vector falls into. Then you could use arrayfun to count the number of students who fell in each grade bin.
Or, if that seems daunting, you could just use a simple loop. Loops are not a terrible thing. Just a little slow if you have too many of them.
0 Comments
Walter Roberson
on 23 Feb 2024
Edited: Walter Roberson
on 23 Feb 2024
Remove all of the if statements. And convert the && to &
A1=sum(total>=90 & total<=100);
fprintf('The number of A+ students is : %d\n',A1)
A=sum(total>=80 & total <90);
fprint('The number of A students is : %d\n',A2)
B=sum(total >= 70 & total<80);
fprintf('The number of B students is %d\n',B)
C=sum(total>=60 & total <70);
fprintf('The number of C students is %d\n',C)
D=sum(total>=50 & total<60);
fprintf('The number of D students is: %d\n',D)
E=sum(total>=40 & total<50);
fprintf('The number of E students is: %d\n',E)
F=sum(total>=0 & total<40);
fprintf('The number of F students is: %d\n',F)
2 Comments
Les Beckham
on 23 Feb 2024
Edited: Les Beckham
on 23 Feb 2024
Because "Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values." while & and | can operate on vectors.
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!