I cannot get my code to output the letter grade as an array. Please help?

조회 수: 1 (최근 30일)
Kyle Skelly
Kyle Skelly 2016년 4월 11일
댓글: Kyle Skelly 2016년 4월 11일
How do I go about getting the code to output the letters of the grades as an array?
grades = [82, 90, 75, 94, 88, 99, 45, 90]
for a=1 : numel(grades)
if grades(a) <= 60
disp( sprintf( '%d = F', grades(a) ) )
elseif grades(a) >=60 && grades<70
disp( sprintf( '%d = D', grades(a) ) )
elseif grades(a) >=70 && grades<80
disp( sprintf( '%d = C', grades(a) ) )
elseif grades(a) >=80 && grades<90
disp( sprintf( '%d = B', grades(a) ) )
else grades(a) >=90
disp( sprintf( '%d = A', grades(a) ) )
end
end
Thanks in advance!

답변 (1개)

Kirby Fears
Kirby Fears 2016년 4월 11일
편집: Kirby Fears 2016년 4월 11일
You can use a character array to store the letter grades for each score in order.
% Here are some grades
scores = [82, 90, 75, 94, 88, 99, 45, 90];
% Set up the grading scale
gradeScale = 'ABCDF';
gradeScaleMinScore = [90,80,70,60,0];
% Identify grade for each score
grades = gradeScale(...
arrayfun(@(g) find(g>=gradeScaleMinScore,1,'first'),scores));
Hope this helps.

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by