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
Ruger28 2019년 10월 31일
What did you try so far?
Daniel M
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
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
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.
Adam Danz
Adam Danz 2019년 10월 31일
편집: Adam Danz 2019년 10월 31일
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
This is the code I wrote but I can't do it anymore I'm getting an error now
function [A, B, C, D, E, F] = fun_degree(inp)
if inp >=90 && inp >=100
[A, B, C, D, E, F]='A';
elseif inp >=89 && inp >= 80
[A, B, C, D, E, F]='B';
elseif inp >=79 && inp >=70
[A, B, C, D, E, F]='C';
elseif inp >=69 && inp >=60
[A, B, C, D, E, F]='D';
elseif inp >=59 && inp >=50
[A, B, C, D, E, F]='E';
elseif inp <50
[A, B, C, D, E, F]='F';
end
end
Adam Danz
Adam Danz 2019년 10월 31일
편집: Adam Danz 2019년 10월 31일
As the error message indicates, you cannot assign a character (or any scalar) to a vector of outputs.
I asked about what your inputs look like but you didn't reply to that question.
Howart Stan
Howart Stan 2019년 11월 1일
A=[100:90]
B=[89:80]
C=[79:70]
D=[69:60]
E=[59:50]
F=[49:0]
the program I want to do for example [52, 81, 82, 90, 15, 92, 42] when i write these numbers
a=
90 92
b=
81 82
c= empty matrix
d= empty matrix
e=
52
f=
15 42
Adam Danz
Adam Danz 2019년 11월 1일
편집: Adam Danz 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).
function [A, B, C, D, E, F] = fun_degree(x)
[a,b,c,d,e,f]=y(x);
if a >=90
A='A';
elseif b >=80
B='B';
elseif c >=70
C='C';
elseif d >=60
D='D';
elseif e >=50
E='E';
elseif f < 50
F='F';
end
end
Error in fun_degree (line 2)
[a,b,c,d,e,f]=deger(inp);
I want to write in this format but I got an error like this. What could be the reason?
Also thank you for help
Adam Danz
Adam Danz 2019년 11월 2일
If that's the form you want, then follow Sulaymon Eshkabilov's example.

댓글을 달려면 로그인하십시오.

답변 (2개)

Adam Danz
Adam Danz 2019년 10월 31일
편집: Adam Danz 2019년 11월 1일

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
Sulaymon Eshkabilov 2019년 10월 31일
편집: Sulaymon Eshkabilov 2019년 10월 31일

1 개 추천

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

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에 대해 자세히 알아보기

질문:

2019년 10월 31일

댓글:

2019년 11월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by