I can't comput the following commands

2 ビュー (過去 30 日間)
Hong
Hong 2024 年 2 月 23 日
編集済み: Les Beckham 2024 年 2 月 23 日
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

回答 (2 件)

John D'Errico
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.

Walter Roberson
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 件のコメント
Hong
Hong 2024 年 2 月 23 日
why i can't use && in this case
Les Beckham
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 ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by