Calculating grades in a while loop

조회 수: 4 (최근 30일)
Angel Sanchez
Angel Sanchez 2020년 2월 13일
댓글: Angel Sanchez 2020년 2월 13일
If I have a array of students grade scores, score=[82, 75, 78, 82, 90, 87], and i want to calculate the number of A's, B's. How would i do that for example to be a grade of A it must be greater or equal to 90, which on the score is shows 2. How can i output that which it will give me the value of 2, as well as for b which would give me 3 which is greater or equlat to 80.

답변 (2개)

Bhaskar R
Bhaskar R 2020년 2월 13일

Jakob B. Nielsen
Jakob B. Nielsen 2020년 2월 13일
You don't need a while loop for that - it is easy with logic indexing, like so:
score=[82 75 78 82 90 87];
A=score(score>=90); %take the indexes of score, for which that same index of score is greater than or equal to 90.
numA=numel(A);
B=score(score>=80 & score<90);
numB=numel(B);
  댓글 수: 3
Jakob B. Nielsen
Jakob B. Nielsen 2020년 2월 13일
Weird homework to ask you to do more than you need. You should be cheeky, and hand in the above solution as well as the loop :D
Must it be a while loop, specifically, or can it be a for loop?
Angel Sanchez
Angel Sanchez 2020년 2월 13일
yes a while loop

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by