Some basic MATLAB questions

Okay, so I'm just starting to use MATLAB for engineering assignments. The assignment from my instructor requires us to write a loop using if, else if and else commands. Thats not what I have trouble with, but I need some assistance to come up with a way to take a number and assign it a letter grade based on the number. So the grading scale is like this:
0-59 - F 60-69 - D 70-79 - C 80-89 - B 90-100 - A
So I know I will start the first condition with if, but could someone do an example to show me how it is supposed to be set up? Please DO NOT do the whole thing, as I want to learn it on my own, but sometimes we need a bit of assistance.
Thank you
EDIT: Thanks to Matt for the first Question.

1 comentario

Naftali
Naftali el 18 de Feb. de 2013
Editada: Walter Roberson el 18 de Feb. de 2013
% n - input numbers in array
% g - output grades
grades = 'FDCBA' ;
k = min(max(floor(0.1*(n-50)+1,1),5) ;
g = grades(k) ;

Iniciar sesión para comentar.

 Respuesta aceptada

Matt Fig
Matt Fig el 11 de Sept. de 2012
Editada: Matt Fig el 11 de Sept. de 2012

0 votos

X = round(rand*6)
if X<3
disp('Small X')
elseif X==3
disp('X is 3')
elseif X>=3 & X<5
disp('Medium X')
else
disp('X is 5 or 6')
end

2 comentarios

nejib
nejib el 21 de Ag. de 2023
Editada: Walter Roberson el 2 de Dic. de 2025
hello :
i need to have the the same amplitude!!!!
any help : !!
t = 0:1/1e3:10;
d = [0:2:60;sin(1*pi*0.1*(0:2:60))]';
x = @rectpuls;
y = pulstran(t,d,x);
plot(t,y)
hold off
xlabel('Time (s)')
ylabel('Waveform')
Same amplitude:
t = 0:1/1e3:10;
d = 0:2:60;
x = @rectpuls;
y = pulstran(t,d,x);
plot(t,y)
hold off
xlabel('Time (s)')
ylabel('Waveform')

Iniciar sesión para comentar.

Más respuestas (8)

Matt Tearle
Matt Tearle el 11 de Sept. de 2012

5 votos

Thank you for your asking such a great question! Matt has basically given you all the info you need for this assignment, but, in the spirit of Sean's "not what you were asked, but some cool stuff you should know about MATLAB"...
x = randi(100,20,1) % Make some grades
y = char(70*ones(size(x))); % Everyone gets an F!
y(x>59) = 'D' % Upgrade the Ds (and better)
y(x>69) = 'C' % Upgrade the Cs
y(x>79) = 'B' % The Bs
y(x>89) = 'A' % And finally the As
Or (requires Stats TB, which you should have as a student)
y = ordinal(x,{'F','D','C','B','A'},[],[0,60:10:100])
hist(y) % See the grades
pie(hist(y)) % Grade distribution
mean(x(y>='C')) % The average score of those who got a C or better

1 comentario

Sean de Wolski
Sean de Wolski el 11 de Sept. de 2012
+1: ordinal can behave like histc(). Learn something every day

Iniciar sesión para comentar.

Matt Fig
Matt Fig el 11 de Sept. de 2012
Editada: Matt Fig el 11 de Sept. de 2012

3 votos

As per requested:
% x is the numerical unknown grade
G = char(interp1([-inf,59,60,69,70,79,80,89,90,inf],['FFDDCCBBAA'],x,'n'))
or, for 2012+ versions:
G = char(interp1([-realmax,59,60,69,70,79,80,89,90,realmax],double(['FFDDCCBBAA']),x,'n'))
Matt Fig
Matt Fig el 11 de Sept. de 2012
Editada: Matt Fig el 11 de Sept. de 2012

2 votos

Another one-liner (short!):
% x is the unknown numerical grade.
G = char(65+sum(x<.5+[59 59:10:89]));
As it stands it only works for scalar x. BSXFUN would cure that if it was needed, like so.
G = char(65+sum(bsxfun(@lt,x,.5+[59 59:10:89].')));
Sean de Wolski
Sean de Wolski el 11 de Sept. de 2012
Editada: Sean de Wolski el 11 de Sept. de 2012

1 voto

I know this is not the purpose of your assignment, but here is a cool way to do it. The logic should be easily transmittable to if/else/etc instead of histc()
letters = 'FDCBA';
grade = 87
[~,idx] = histc(grade,[0 59:10:89 100+eps(100)]);
letters(idx)

5 comentarios

Matt Fig
Matt Fig el 11 de Sept. de 2012
Editada: Matt Fig el 11 de Sept. de 2012
I like this one too!
char(interp1([-inf,59,60,69,70,79,80,89,90,inf],['FFDDCCBBAA'],87,'n'))
Sean de Wolski
Sean de Wolski el 11 de Sept. de 2012
I like it! Interestingly though, this no longer works in 12a.
Hump-day challenger tomorrow?
Jan
Jan el 11 de Sept. de 2012
Editada: Jan el 11 de Sept. de 2012
What is the error message in R2012a? But this does still work, I hope:
char(interp1([-inf,59,60,69,70,79,80,89,90,inf], double('FFDDCCBBAA'), 87, 'n'))
Please, Matt Fig, post your one-lines as an answer.
Sean de Wolski
Sean de Wolski el 11 de Sept. de 2012
Inf and Nan are non supported. Converting them to reasonable values, strings are not supported.
Matt Fig
Matt Fig el 11 de Sept. de 2012
Now why would support for inf be dropped? TMW does some strange stuff from time to time. (strmatch, findstr, etc...)

Iniciar sesión para comentar.

Jan
Jan el 11 de Sept. de 2012

1 voto

index = max(1, ceil(-4.9:0.1:5.1));
pool = 'FDCBAA';
Grade = pool(index(x + 1));
Yash Meshram
Yash Meshram el 24 de Oct. de 2019

0 votos

i = input('Marks of the student: ');
if 0<=i && i<=59
fprintf('F grade\n');
end
if 60<=i && i<=69
fprintf('D grade\n');
end
if 70<=i && i<=79
fprintf('C grade\n');
end
if 80<=i && i<=89
fprintf('B grade\n');
end
if 90<=i && i<=100
fprintf('A grade\n');
end
hassan saher
hassan saher el 24 de Dic. de 2020

0 votos

Use help command to get help about rand function.
Vivek Sharma
Vivek Sharma el 8 de Jun. de 2021

0 votos

Please help me with the MATLAB code for the following problem. PDF is attached herewith.
Thank you.

1 comentario

mahmood
mahmood el 17 de Mayo de 2023
Editada: DGM el 18 de Mayo de 2023
clc;
clear;
% Constants
c = 13.03;
p = 19.93;
% Initial temperatures
T1 = 576;
T2 = 439;
% Number of impacts
m = 5;
% Initialize variables
T = (T1 + T2) / 2;
E = zeros(m, 1);
% Perform m impacts
for i = 1:m
E(i) = (c * p * T) / 23.5;
T = (T1 + T) / 2;
end
% Display the values of E for each impact
disp('Values of E for each impact:');
disp(E);

Iniciar sesión para comentar.

Categorías

Preguntada:

el 11 de Sept. de 2012

Comentada:

el 2 de Dic. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by