Convert an array of numbers into letters using logical indexing

5 次查看(过去 30 天)
I am working on a school project that involves reading an excel file, calculating grades for each cell, replacing number grades with letter grades, and writing the array back into another excel file. I'm hung up on the replacing number grades with letter grades part. This is what I have so far for that portion:
grades =
6.29
5.87
5.28
7.38
7.4
4.07
4.23
7.25
3.6
4.37
4.74
4.79
7.73
9.15
8.83
5.23
0.66
6.18
7.71
8.16
6.33
5.46
6.63
0.07
grades(grades>=9.0 & grades<=10.0) = 'A';
grades(grades>=8.5 & grades<9.0) = 'AMinus';
grades(grades>=8.0 & grades<8.5)= 'BPlus';
grades(grades>=7.5 & grades<8.0) = 'B';
grades(grades>=7.0 & grades<7.5) = 'BMinus';
grades(grades>=6.5 & grades<7.0) = 'CPlus';
grades(grades>=6.0 & grades<6.5) = 'C';
grades(grades>=5.5 & grades<6.0) = 'CMinus';
grades(grades>=5.0 & grades<5.5) = 'D';
studentAvg(studentAvg>=0 & studentAvg<5.0) = 'F';
So I've indexed the array that I've been given, and my logical indexing code determines which grades in the array are A's,B's,C's, and so on.
The problem I've run into, though, is that after logical indexing, I can't figure out how to write 'A' back into the array to the number grades which have earned A's, 'B' to those that earned B's, and so on.
I need to keep the order of my array as it is linked to a list of Student ID numbers that I don't want to resort/reindex.
Any help is much appreciated!
  2 个评论
Weston Way
Weston Way 2019-3-15
This is what I'd like it to look like, with a few more values where the '...' is.
gradesSolved =
91180 C
91737 D
92024 C+
'...'
99902 F

请先登录,再进行评论。

回答(2 个)

per isakson
per isakson 2019-3-15
编辑:per isakson 2019-3-15
Another approach
>> grades = reshape([ 6.29, 5.87, 5.28, 7.38, 7.40, 4.07, 4.23, 7.25, 3.60 ...
, 4.37, 4.74, 4.79, 7.73, 9.15, 8.83, 5.23, 0.66, 6.18 ...
, 7.71, 8.16, 6.33, 5.46, 6.63, 0.07 ], [],1 );
>> out = cssm( grades )
out =
1×24 cell array
Columns 1 through 9
{'C'} {'C-'} {'D'} {'B-'} {'B-'} {'F'} {'F'} {'B-'} {'F'}
Columns 10 through 19
{'F'} {'F'} {'F'} {'B'} {'A'} {'A-'} {'D'} {'F'} {'C'} {'B'}
Columns 20 through 24
{'B+'} {'C'} {'D'} {'C+'} {'F'}
where
function out = cssm( grades )
%%
grade_edges = [ 0.0, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 10.0 ];
letter_grades = {'F', 'D', 'C-','C', 'C+','B-','B', 'B+','A-','A' };
%%
ix = discretize( grades, grade_edges );
out = letter_grades( 1, ix );
end
The five digits numbers, e.g. 91737, are they student IDs and where do they come from?

Andrei Bobrov
Andrei Bobrov 2019-3-15
v = [0, 5:.5:10]';
S = string(cellstr(('F':-1:'A')'));
K = S(4:6)' + {'-','','+'}';
K = [S(1:3);K(1:end-1)'];
out = discretize(grades,v,K);

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by