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 comentarios

Ruger28
Ruger28 el 31 de Oct. de 2019
What did you try so far?
Daniel M
Daniel M el 31 de Oct. de 2019
It would probably make more sense to return a cellstring same length as values containing the letter grade as a char.
Ruger28
Ruger28 el 31 de Oct. de 2019
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 el 31 de Oct. de 2019
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 el 31 de Oct. de 2019
Editada: Adam Danz el 31 de Oct. de 2019
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 el 31 de Oct. de 2019
Editada: Adam Danz el 31 de Oct. de 2019
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 el 1 de Nov. de 2019
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 el 1 de Nov. de 2019
Editada: Adam Danz el 1 de Nov. de 2019
@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 el 2 de Nov. de 2019
If that's the form you want, then follow Sulaymon Eshkabilov's example.

Iniciar sesión para comentar.

Respuestas (2)

Adam Danz
Adam Danz el 31 de Oct. de 2019
Editada: Adam Danz el 1 de Nov. de 2019
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 el 31 de Oct. de 2019
Editada: Sulaymon Eshkabilov el 31 de Oct. de 2019
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 comentario

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

Iniciar sesión para comentar.

Categorías

Más información sobre Animation en Centro de ayuda y File Exchange.

Preguntada:

el 31 de Oct. de 2019

Comentada:

el 2 de Nov. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by