Info
此问题已关闭。 请重新打开它进行编辑或回答。
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 次查看(过去 30 天)
显示 更早的评论
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 个评论
James Tursa
2016-10-12
编辑:James Tursa
2016-10-12
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
2016-10-13
编辑:dpb
2016-10-13
"... 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.
回答(1 个)
Soma Ardhanareeswaran
2016-10-20
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 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!