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!
  2 件のコメント
Roger Stafford
Roger Stafford 2016 年 4 月 11 日
Everybody gets a 'B'?
Kyle Skelly
Kyle Skelly 2016 年 4 月 11 日
oops, fixed it

サインインしてコメントする。

回答 (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 Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by