day of the week in a month

Write a function dayName that consumes a parameter, day, containing the numerical value of a day in April 2012. Your function should return the name of that day as a string. For example: dayName(24) should return 'Tuesday'. Test your program on two cases: dayName(18) and dayName(27).
This is what the code that i ran on matlab it gave me an error i dont understand why
function results = dayName(x)
if (x= 2 9 16 23 30)
results = 'Monday' ;
elseif (x= 3 10 17 24)
results = 'Tuesday' ;
elseif (x= 4 11 18 25)
results = 'Wednesday' ;
elseif (x= 5 12 19 26)
results = 'Thursday' ;
elseif (x= 6 13 20 27)
results = 'Friday' ;
elseif (x=7 14 21 28)
results = 'Saturday');
elseif (x= 1 8 15 22 29)
results = 'Sunday');
end
else
end

 Respuesta aceptada

Honglei Chen
Honglei Chen el 25 de Abr. de 2012

0 votos

You want to use
if any(x==[2 9 16 23 30])
and so on. Also at the bottom, you seem to have an extra else hanging around.

3 comentarios

sam
sam el 25 de Abr. de 2012
i put this as my code but i still do not get the correct answer in the command window?
function disp = dayName(x)
if any(x== [2 9 16 23 30])
disp = 'Monday' ;
elseif (x== [3 10 17 24])
disp = 'Tuesday' ;
elseif (x== [4 11 18 25])
disp = 'Wednesday' ;
elseif (x== [5 12 19 26])
disp = 'Thursday' ;
elseif (x== [6 13 20 27])
disp = 'Friday' ;
elseif (x==[7 14 21 28])
disp = 'Saturday';
elseif (x== [1 8 15 22 29])
disp = 'Sunday';
end
end
sam
sam el 25 de Abr. de 2012
never mind i got the answer thank you soo much for the help
Jan
Jan el 25 de Abr. de 2012
Do not forget the "any" in the IF expressions.

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 25 de Abr. de 2012

0 votos

This is a homework question, therefore I give a hint for another solution only.
1. You can create the list of names statically:
Names = {'Monday', 'Tuesday', 'Wednesday', 'Thursday', ...
'Friday', 'Saturday', 'Sunday'};
2. If x is 2, you want to get the 1st element of the cell string.
3. If x is 9, you want to get the same value as for 2. The mod and rem functions can be used to compute the remainder of a division.

Etiquetas

Preguntada:

sam
el 24 de Abr. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by