I can't comput the following commands

조회 수: 3 (최근 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 CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by