Return average of grades as a column vector?

2 views (last 30 days)
Oah Joan
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:
>> avggrade(s,2)
ans =
91.5
MY FUNCTION:
function avg = avggrade(s,nth)
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:
>> avggrades(s)
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
avg = avggrades(s)
Please help! I am stuck! Thank you!"
  2 Comments
Stephen23
Stephen23 on 20 Nov 2020
Edited: John Kelly on 15 Jan 2021
Original question on 16 Nov 2018:
Return average of grades as a column vector?
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:
>> avggrade(s,2)
ans =
91.5
MY FUNCTION:
function avg = avggrade(s,nth)
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:
>> avggrades(s)
ans =
78
91.5
80
Note that you’ll need to use a for loop because MATLAB’s : notation doesn’t work on cell array!
MY FUNCTION:
function avg = avggrades(s)
Please help! I am stuck! Thank you!

Sign in to comment.

Answers (3)

Adam Danz
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 Comments
Oah Joan
Oah Joan on 16 Nov 2018
I am required to use a for loop. Can you help please?
Adam Danz
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.

Sign in to comment.


Steven Lord
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
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 letter grade.
avg = avggrade(s,nth)
% Get average numerical score of each student.
averageScore = avggrades(s)
function averageGrade = avggrade(s,nth)
% Extract the letter grade that the student was given,
% presumably based on the average of the numerical scores.
averageGrade = strtrim(s{nth, 2});
end
function averageScore = avggrades(s)
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.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!