I can't comput the following commands
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
John D'Errico
2024-2-23
编辑:John D'Errico
2024-2-23
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 个评论
Walter Roberson
2024-2-23
编辑:Walter Roberson
2024-2-23
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 个评论
Les Beckham
2024-2-23
编辑:Les Beckham
2024-2-23
Because "Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values." while & and | can operate on vectors.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Genomics and Next Generation Sequencing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!