Function of vector with letter grade
이전 댓글 표시
function [A, B, C, D, E, F] = grade(value)
end
I want to make a letter note calculation using vector, but it shows only one number in the code I write, it works when I enter the number (90) but [(70,89,100]) how can I do if I can't get results in numbers?
댓글 수: 11
Ruger28
2019년 10월 31일
What did you try so far?
Daniel M
2019년 10월 31일
It would probably make more sense to return a cellstring same length as values containing the letter grade as a char.
Ruger28
2019년 10월 31일
You said you had something, so what have you tried? No one is going to do it for you.
John D'Errico
2019년 10월 31일
Please don't use answers just to make a plea for help. You need to show what you are doing, as otherwise, we are just doing your work and thinking for you. You learn nothing then.
In order to better describe the problem, you could provide some input-output pairs that would show what the function is supposed to be doing. For example, one interpretation of your question would be that your input is a vector of percentage grades and your output is a single letter grade in the form of a chracter ('A') or a string ("A"). Another interpretation would be that your input is a vectof of percentage grades and your output is an array of strings/chars in the form {'A','B','F'} or ["A","B","F"].
By showing us what you've already tried (suggested above) we would get a better understanding of what you're trying to do and where that's going wrong.
More important lesson here: Proposing a solution to this problem would probably just take a few seconds and 1 line of code but there has already been 6 hours since the first comment to this problem and 4 different people chipping in. That's because the problem is not well defined.
"If I had only one hour to save the world, I would spend fifty-five minutes defining the problem, and only five minutes finding the solution." -Albert Einstein
Howart Stan
2019년 10월 31일
Howart Stan
2019년 11월 1일
@Howart Stan, the bigger lesson to be learned here is to clearly define your problem before volunteers start working on it ;)
See my updated answer (at the bottom).
Howart Stan
2019년 11월 1일
Adam Danz
2019년 11월 2일
답변 (2개)
Here, you can enter any type of numeric input from scalars, vectors, matricies, to n-dimensional arrays and the output will be the same shape.
function GRADE = fun_degree(inp)
grp = discretize(inp,[-inf,50:10:90,inf]);
letters = {'F' 'E' 'D' 'C' 'B' 'A'};
GRADE = letters(grp);
end
Test it...
inp = [0:10:100, 22 33 44 55 66 77 88 99] % 1 x 19 vector
GRADE = fun_degree(inp)
% {'F'} {'F'} {'F'} {'F'} {'F'} {'E'} {'D'} {'C'} {'B'} {'A'} . . . . .
inp = reshape(45:100, 7,8); % 7 x 8 matrix
GRADE = fun_degree(inp)
% GRADE =
% 7×8 cell array
% {'F'} {'E'} {'E'} {'D'} {'C'} {'B'} {'B'} {'A'}
% {'F'} {'E'} {'D'} {'D'} {'C'} {'B'} {'B'} {'A'}
% {'F'} {'E'} {'D'} {'D'} {'C'} {'B'} {'B'} {'A'}
% {'F'} {'E'} {'D'} {'D'} {'C'} {'B'} {'A'} {'A'}
% {'F'} {'E'} {'D'} {'C'} {'C'} {'B'} {'A'} {'A'}
% {'E'} {'E'} {'D'} {'C'} {'C'} {'B'} {'A'} {'A'}
% {'E'} {'E'} {'D'} {'C'} {'C'} {'B'} {'A'} {'A'}
inp = 98.5;
GRADE = fun_degree(inp)
% GRADE =
% 1×1 cell array
% {'A'} %Cell array
GRADE = GRADE{:};
% GRADE =
% 'A' % Char
[addendum]
Here's the modified function to output the number of As, Bs, etc...
function [A, B, C, D, E, F, GRADE] = fun_degree(inp)
grp = discretize(inp,[-inf,50:10:90,inf]);
letters = {'F' 'E' 'D' 'C' 'B' 'A'};
GRADE = letters(grp);
F = sum(grp(:)==1);
E = sum(grp(:)==2);
D = sum(grp(:)==3);
C = sum(grp(:)==4);
B = sum(grp(:)==5);
A = sum(grp(:)==6);
end
Sulaymon Eshkabilov
2019년 10월 31일
편집: Sulaymon Eshkabilov
2019년 10월 31일
Hi,
function GRADE = fun_degree(inp)
if inp >=90
GRADE='A';
fprintf('Your score is %d and Your earned grade is A \n', inp)
elseif inp <90 && inp >= 80
GRADE='B';
fprintf('Your score is %d and Your earned grade is B \n', inp)
elseif inp <80 && inp >=70
GRADE='C';
fprintf('Your score is %d and Your earned grade is C \n', inp)
elseif inp <70 && inp >=60
GRADE='D';
fprintf('Your score is %d and Your earned grade is D \n', inp)
elseif inp <60 && inp >=50
GRADE='E';
fprintf('Your score is %d and Your earned grade is E \n', inp)
else % inp <50
GRADE='F';
fprintf('Your score is %d and Your earned grade is F. Sorry you have failed :( \n', inp)
end
end
A shorter version as mentioned:
function GRADE = fun_degree(inp)
if inp >=90
GRADE='A';
elseif inp <90 && inp >= 80
GRADE='B';
elseif inp <80 && inp >=70
GRADE='C';
elseif inp <70 && inp >=60
GRADE='D';
elseif inp <60 && inp >=50
GRADE='E';
else % inp <50
GRADE='F';
end
fprintf('Your score is %2.2f and Your earned grade is %c \n', inp, GRADE)
end
Good luck.
댓글 수: 1
Adam Danz
2019년 10월 31일
The update is much better. This output might be cleaner
fprintf('Your score is %.3g and Your earned grade is %c \n', inp, GRADE)
% ^^^^
Compare:
Your score is 89.5 and Your earned grade is B % with %.3g
Your score is 8.950000e+01 and Your earned grade is B % with %d
카테고리
도움말 센터 및 File Exchange에서 Animation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
