MATLAB Answers

Moe
0

Need help coding a grading system to a data set.

Asked by Moe
on 5 Oct 2017
Latest activity Answered by Moe
on 7 Oct 2017

So I have a data set of grades from Excel that i'm putting in matlab. In Matlab my objective is to find the average of each student, then assign grades to each student based on their average using if-then statements. I'm having trouble with the assigning grades to each student part. I need help assigning grades to each student then using the display command to show it. I'll put in my dataset(incase it helps), the names don't come into matlab from excel but that's ok.

   66    66    67    68    92
    90    90    90    90    90
    75    80    95    65    75
    75    45    25    70    70
    75    90    90    90    95
    85    88    76    88    90
    90    80    50    80    90
   100   100    50    50   100
    55    65    66    68    75
    65    90    90    60    50
   100    40    60    60    70
    55   100    95    88    90
    75    80    85    77    82
    80    82    88    84    86
    66    68    64    70    62
    90    92    88    75    90
   100    40    60    60    70
    55   100    95    88    90
    75    80    85    77    82
    80    82    88    84    86
    66    68    64    70    62
    90    92    88    75    90
    75    90    67    68    92
    85    88    90    90    90
    90    80    95    65    75
   100   100    25    70    70
    55    65    90    90    95
    65    90    76    88    90
   100    40    50    80    90
    55   100    50    50   100

Everything after "Student_Average = mean(data,2);" is where i'm not getting it right.

>> data = xlsread ('Exam_Grades_Data(2).xlsx');  %Loads data into matlab
Student_Average = mean(data,2);  % Calculates average of each row(Student)
grade = Student_Average;
if n>=90
    grade = 'A';
elseif n >= 80
	grade ='B';
elseif n >= 70
	grade ='C';
elseif n >= 60
	grade ='D';
elseif n <=59
    grade = 'E';
end

  0 Comments

Sign in to comment.

4 Answers

Answer by James Tursa
on 5 Oct 2017
Edited by James Tursa
on 5 Oct 2017

The averages you calculate are in a column vector called Student_Average, but your subsequent code does not even use that variable. So you need to alter your code to use that variable.

Also, for a beginner, I would advise using a simple for loop to calculate the grades result, which it appears you want to be a char variable.

So an outline of your code would be:

Student_Average = etc.
for k=1:numel(Student_Average)
    % YOU FILL IN CODE HERE TO CALCULATE GRADE
end

The code you fill in above is going to be your if-then-else stuff you show above. You will calculate the grade based on Student_Average(k), and you will assign the result to grade(k).

And your current code doesn't assign anything if the average is between 59 and 60. So I would advise changing this line

elseif n <=59

to this instead

else

(Side note: What is an 'E' grade?)

  0 Comments

Sign in to comment.


Answer by Akira Agata
on 5 Oct 2017
Edited by Akira Agata
on 5 Oct 2017

Using table type variable and discretize function, you can discretize the average score and assign 'A' - 'E' for each bin, like:

% Sample data
data = array2table(randi([50,100],10,5));
% Calculate mean for each row
data.Mean = mean(data{:,:},2);
% Discretize the mean value and assign categorical value
data.grade = discretize(data.Mean,...
  [0, 60, 70, 80, 90, 100],...
  'categorical',...
  {'E','D','C','B','A'});

The output is as follows.

data =
10×7 table
  Var1    Var2    Var3    Var4    Var5    Mean    grade
  ____    ____    ____    ____    ____    ____    _____
  84      62      92       97     61      79.2    C    
  77      56      92       82     55      72.4    C    
  71      80      63       74     55      68.6    D    
  82      72      81       82     53        74    C    
  83      73      79       77     70      76.4    C    
  84      83      77       83     72      79.8    C    
  82      89      94       77     68        82    B    
  98      67      63       86     88      80.4    B    
  60      83      66       76     82      73.4    C    
  86      71      56      100     89      80.4    B    

  0 Comments

Sign in to comment.


Answer by Chad Greene
on 5 Oct 2017

I'd skip the if statement entirely and treat it like a histogram. You can use histcounts to get to bin the averages. Something like this:

Student_Average = mean(data,2);  % Calculates average of each row(Student)
% Grades: 
grades = {'F','D','C','B','A'}; 
% Corresponding bin edges of each grade: 
edges = [0 60 70 80 90 Inf]; 
% Indices of which bin each Student_Average corresponds to:
[~,~,ind] = histcounts(Student_Average,edges);
% Letter grades for all students: 
grade = grades(ind)'
% Make a nice figure: 
histogram(ind,.5:5.5)
set(gca,'xtick',1:5,'xticklabel',grades)
box off 
axis tight

  0 Comments

Sign in to comment.


Answer by Moe
on 7 Oct 2017

Ok one thing I needed to clarify is that for this assignment, I HAVE to use if then else, statements. It's the whole reason for this assignment, so that later on we can learn easier quicker ways to do it. Here's my updated code to show where i'm at.

%Loads data into matlab data = xlsread ('Exam_Grades_Data(2).xlsx');

%Calculates average of each row(Student) Student_Average = mean(data,2);

if Student_Average >=90 grade = 'A'; elseif Student_Average >= 80 grade ='B'; elseif Student_Average >= 70 grade ='C'; elseif Student_Average >= 60 grade ='D'; else grade ='E'; end

display(grade)

The final goal is to be able to display the student averages and final grade(in letter format), in the command window.

  0 Comments

Sign in to comment.