Return average of grades as a column vector?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
"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 Commenti
Stephen23
il 20 Nov 2020
Modificato: John Kelly
il 15 Gen 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!
Risposte (3)
Adam Danz
il 16 Nov 2018
Modificato: Adam Danz
il 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 Commenti
Adam Danz
il 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
il 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.
0 Commenti
Image Analyst
il 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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Performance and Memory in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!