# How to define the step increment of iteration inside the loop

조회 수: 111 (최근 30일)
Mekala balaji 2019년 6월 1일
댓글: Mekala balaji 2019년 6월 1일
Hi,
I want increment the iteration 'i' by 3 on each iteration completes and the next iteration statrt from the incremented 'i'.
Code working:
A=[1;2;4;6;7;11;78;12;32;14;32;45];
for i=1:3:size(A,1)
disp(i)
% i=i+3;
% disp(i)
disp('.....')
end
but when I use below code, it does not works
A=[1;2;4;6;7;11;78;12;32;14;32;45];
increment=3;
for i=1:size(A,1)
disp(i)
i=i+increment;
%disp(i)
% disp('.....')
end
can't we define the increment like above?
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Stephen23 2019년 6월 1일
"can't we define the increment like above?"
Nope. You already defined the iteration variable in the for loop definition. What happens inside the loop does not change the for loop definition.

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

### 채택된 답변

KALYAN ACHARJYA 2019년 6월 1일
편집: KALYAN ACHARJYA 2019년 6월 1일
In your second cod the iteration steps depends on i, its changing changing the step size 1 as defined in for loop statement
for i=1:size(A,1) %it means i=1,2,3,4...
Replace it with
for i=1:increment:size(A,1)
disp(i);
end
The following vector is same as above
i=1:3:size(A,1);
Which is definitely same as first code.
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Mekala balaji 2019년 6월 1일
Very clear, thanks a lot.

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

### 카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

R2017a

### Community Treasure Hunt

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

Start Hunting!

Translated by