Change step value within a for loop.

조회 수: 4 (최근 30일)
Keqiao Li
Keqiao Li 2016년 7월 26일
편집: Azzi Abdelmalek 2016년 7월 26일
Hi
for i = 1:10
i
i = 6;
end
what I'm trying to do is let i = 1, 6, 7, 8, 9,10, but instead, MATLAB shows that 1,2,3,4,5,6,7,8,9,10. is there any way to do that? I'm aware that in a for loop, the "end" will reset the step value. Please help me to solve this problem

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 26일
편집: Azzi Abdelmalek 2016년 7월 26일
for i = [1 6:10]
y=i
end
  댓글 수: 2
Keqiao Li
Keqiao Li 2016년 7월 26일
편집: Azzi Abdelmalek 2016년 7월 26일
Hi Azzi, thank you for the answer. What if I let i = 1:100, and want to have i = 1,6,19,30, 61,89,100. I know I can just let i = [1,6,19,30, 61,89,100]. But I'm expecting that this can be done in a for loop, just like what I'm asking
Thanks Li
Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 26일
If you want i = 1,6,19,30, 61,89,100 then write
i = [1,6,19,30, 61,89,100]
I don't understand what is your problem with that. It don't work like you are asking, because this is how for loop works in Matlab !

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by