# Return average of grades as a column vector?

9 views (last 30 days)
Oah Joan on 16 Nov 2018
Edited: John Kelly on 15 Jan 2021
"Hello, I am a first year computing science student and these two questions were "to-trys" on my notes during lecture. I completed the first one, but I am having trouble with the second one! These question involve cell arrays.
(1) Write a function avggrade(s,nth) that computes the average grade of the nth student when the data is enter in the same format as:
s = {'john' 'B+' [ 75 79 80]; 'judith' 'A' [91 92]; ' Tom ' ' B ' [76 82 83 79]}
GIVEN TEST:
ans =
91.5
MY FUNCTION:
x = s{nth,3};
avg = mean(x);
end
But I do not understand how to do the second question!
(2) Write a function avggrades(s) that returns a column vector of the average of the numeric grades for each student.
Where
s = {'john' 'B+' [ 75 79 80]; 'judith' 'A' [91 92]; ' Tom ' ' B ' [76 82 83 79]}
GIVEN TEST:
ans =
78
91.5 \t\t\t\t\t\t
80
Note that you’ll need to use a for loop because MATLAB’s : notation doesn’t work on cell array!
MY FUNCTION:
function
##### 2 CommentsShowHide 1 older comment
Rena Berman on 15 Jan 2021

Adam Danz on 16 Nov 2018
Edited: Adam Danz on 16 Nov 2018
You don't need a for-loop. The function cellfun() applies a function to all elements of a cell array. Check this out and apply it to your solution.
doc cellfun
cellfun(@mean, s(:,3))
##### 2 CommentsShowHide 1 older comment
Adam Danz on 16 Nov 2018
My suggestion converted to a for-loop looks like this.
for i = 1:size(s,1)
m(i) = mean(s{i, 3});
end
You can use that to help write your code.

Steven Lord on 16 Nov 2018
You have written a function to compute the average grade for one specified student.
Make a vector that has the same number of elements as you have students. The size and zeros functions will help you.
Use a loop and fill each element of that vector with the average for the corresponding student: the first element with the average for the first student, the second element with the second student, etc.

Image Analyst on 16 Nov 2018
If you want to use a for loop, this will work:
% Define data.
s = {'john' 'B+' [ 75 79 80]; 'judith' 'A' [91 92]; ' Tom ' ' B ' [76 82 83 79]}
% Get third students grade -- average letter grade based on several numerically scored tests.
nth = 3;
% Get average numerical score of each student.
% Extract the letter grade that the student was given,
% presumably based on the average of the numerical scores.
end
averageScore = zeros(size(s, 1), 1);
for row = 1 : size(s, 1)
% Extract the third cell in the row, which is an array,
% and compute the mean of the numerical scores.
averageScore(row) = mean(s{row,3});
end
end
Note that I call the "grade" the letter, (as opposed to scores which are the numbers), and they appear to have an average grade in column 2, which is already the average of the individual test scores. Whether each score had it's own grade, or they just gave one grade based on either the sum or average of the numerical scores, we don't know. Regardless, it doesn't matter, so I just extracted the average grade from column 2.
For the next question you extract column 3 for each student and compute the mean for each student and store it in a column vector in the corresponding row.