Return average of grades as a column vector?

6 次查看(过去 30 天)
"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 个评论
Stephen23
Stephen23 2020-11-20
编辑:John Kelly 2021-1-15
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!

请先登录,再进行评论。

回答(3 个)

Adam Danz
Adam Danz 2018-11-16
编辑:Adam Danz 2018-11-16
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 个评论
Oah Joan
Oah Joan 2018-11-16
I am required to use a for loop. Can you help please?
Adam Danz
Adam Danz 2018-11-16
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
Steven Lord 2018-11-16
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 2018-11-16
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.

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by