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

3 次查看(过去 30 天)
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 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by