필터 지우기
필터 지우기

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
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
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
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!

Translated by