Function of vector with letter grade

10 views (last 30 days)
Howart Stan
Howart Stan on 31 Oct 2019
Commented: Adam Danz on 2 Nov 2019
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 Comments
Howart Stan
Howart Stan on 1 Nov 2019
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 on 2 Nov 2019
If that's the form you want, then follow Sulaymon Eshkabilov's example.

Sign in to comment.

Answers (2)

Adam Danz
Adam Danz on 31 Oct 2019
Edited: Adam Danz on 1 Nov 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 on 31 Oct 2019
Edited: Sulaymon Eshkabilov on 31 Oct 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 Comment
Adam Danz
Adam Danz on 31 Oct 2019
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

Sign in to comment.

Categories

Find more on MATLAB Mobile Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!