Info
この質問は閉じられています。 編集または回答するには再度開いてください。
Make a loop in such way that i will be increasing by 5 from 4 up to 30 and by 3 from 30 till up to 50.
1 回表示 (過去 30 日間)
古いコメントを表示
im still confused on using while loops and if loops can someone help me with this question
2 件のコメント
Rik
2019 年 10 月 7 日
It is extremely rude to edit away your question, especially if you received an answer from someone who took the time to read and understand your question and then spent time finding a solution and posting an answer. If you want private help, hire a consultant.
回答 (1 件)
Walter Roberson
2019 年 10 月 7 日
for VariableName = [4:5:30,30:3:50]
Note that in a case like this, you should always check in case you accidentally repeat a value. For example, [4:13:30, 30:3:50] would start [4, 17, 30] and then continue from [30 33 36 ...] . It so happens that 4:5:30 stops at 29 and so there is no duplication of 30.
1 件のコメント
Walter Roberson
2019 年 10 月 7 日
for VariableName = [4:5:30,30:3:50]
something
end
is the same as
for VariableName = [4:5:30]
something
end
for VariableName = [30:3:50]
something
end
In turn,
for VARIABLENAME = FIRST_VALUE : INCREMENT : FINALVALUE
something
end
for positive increment is the same as (for your needs, but some of the details differ)
VARIABLENAME = FIRST_VALUE;
while VARIABLE_NAME <= FINALVALUE
something
VARIABLENAME = VARIABLENAME + INCREMENT;
end
So you can simply write two while loops in a row.
However, you can also code something like,
VARIABLENAME = FIRST_VALUE;
first_phase = true;
while VARIABLE_NAME <= FINALVALUE
something
if first_phase
VARIABLENAME = VARIABLENAME + FIRSTINCREMENT;
if VARIABLENAME > BREAKPOINT
VARIABLENAME = BREAKPOINT;
first_phase = false;
end
else
VARIABLENAME = VARIABLENAME + SECONDINCREMENT;
end
end
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!