Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
I honestly have no clue where to start with this much help is needed and I really can't figure out how to put an "if" statement inside a switch statement?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
2. A vector, x, is given below.
a) set up a "for" loop to run over all elements of x - use the "length" command
b) inside the "for" loop, use an "if" construct to find any value of x that is less than or equal to 5 and to find any value of x that is greater than 5.
c) for the elements of x that are less than or equal to 5, use a "switch" construct to make that element equal to its square if the element equals to 5; otherwise, make the element of x equal to 1
d) for the x values that are greater than 5, make those elements of x equal to zero.
fprintf('\n\n#2\n')
clear all
x=[1 9 2 8 3 7 4 6 5];disp(x);
2 comentarios
James Tursa
el 12 de Oct. de 2016
Editada: James Tursa
el 12 de Oct. de 2016
What have you done so far? What specific problems are you having with your code? Do you know how to write a for-loop? A switch-statement? An if-test?
dpb
el 13 de Oct. de 2016
Editada: dpb
el 13 de Oct. de 2016
"... how to put an "if" statement inside a switch statement?"
Actually, the switch block is to go inside an if construct the way the assignment is written.
As for where to start, do the first step a). Then attack the next. It's pretty straightforward if you just follow the instructions step-by-step.
Respuestas (1)
Soma Ardhanareeswaran
el 20 de Oct. de 2016
Semantically, 'switch' statement is a collection of if-else-if statements. But if you still want an 'if' inside a switch case, here is a quick example:
switch n
case -1
if n/(-1)==1
disp('inside if')
end
case 0
disp('zero')
case 1
disp('positive one')
otherwise
disp('other value')
end
0 comentarios
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!