# I can't comput the following commands

2 visualizaciones (últimos 30 días)
Hong el 23 de Feb. de 2024
Editada: Les Beckham el 23 de Feb. de 2024
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 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (2)

John D'Errico el 23 de Feb. de 2024
Editada: John D'Errico el 23 de Feb. de 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 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Walter Roberson el 23 de Feb. de 2024
Editada: Walter Roberson el 23 de Feb. de 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 comentariosMostrar NingunoOcultar Ninguno
Hong el 23 de Feb. de 2024
why i can't use && in this case
Les Beckham el 23 de Feb. de 2024
Editada: Les Beckham el 23 de Feb. de 2024
Because "Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values." while & and | can operate on vectors.

Iniciar sesión para comentar.

### Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by