Azzera filtri
Azzera filtri

Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

When using a matrix as input, all conditions are not met? Why?

1 visualizzazione (ultimi 30 giorni)
function gradesFinal = computeFinalGrades(grades)
%computeFinalGrades calculates the final grade
%
%Input grades: a NxM matrix, where N is the number of students and M is
% number of assigments
%Output gradesFinal: a vector with length N, which contains the final grade
% for each of N students
%Calculation of the final grade, rules:
% 1. only one assignment (M=1): the final grade is the grade that has
% been given for that assigment
% 2. if there are two or more assigments (M>1): lowest grade is not
% included. Final grade is the mean of M - 1 highest grades, rounded
% to nearest grade on scale, using function: roundGrade
% 3. if -3 has been given, no matter how many assigments the final grade
% is -3.
%Find the dimension of the input grades
[N M] = size(grades);
i = 1:M;
% The logical vector to include the data:
includedData = true(size(grades, 1), 1);
% The three ways to calculate the final grade:
if M == 1
includedData = grades(i,M);
gradesFinal = roundGrade(includedData);
elseif any(grades == -3)
gradesFinal = -3;
elseif M > 1
includedData = (sum(grades')-min(grades'))/(M-1);
gradesFinal = roundGrade(includedData);
end
end
The function works fine when i am using a vector as input, then it comes out with the final grade and the conditions also works. But when i use a matrix (what the input actually should be), it won't come out as -3 if N has -3 in it... What do this code lack?
Ex. computeFinalGrades([-3 3 7 5 4.5]) --> out: -3 With matrix: mat =
2.0000 3.0000 4.0000 -3.0000
5.0000 0.6000 2.0000 5.5000
2.0000 5.0000 9.0000 8.0000
Output: ans =
4 4 7
But actually it should be: -3 4 7
"roundGrade" is another function that l made

Risposte (0)

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by