This question is closed. Reopen it to edit or answer.

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 view (last 30 days)
Grayson Everhardt
Grayson Everhardt on 12 Oct 2016
Closed: MATLAB Answer Bot on 20 Aug 2021
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.
clear all
x=[1 9 2 8 3 7 4 6 5];disp(x);
dpb on 13 Oct 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.

Answers (1)

Soma Ardhanareeswaran
Soma Ardhanareeswaran on 20 Oct 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')
case 0
case 1
disp('positive one')
disp('other value')


Community Treasure Hunt

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

Start Hunting!

Translated by