# How can ı set initial value about for statement ???

78 views (last 30 days)
Fatih on 6 Aug 2014
Commented: dpb on 6 Aug 2014
Hi !
for i = initial value(normally 1):1:100
for y = 1 : 1: 100
% I calculate something in this for and I understand that I m not in true way.
% And I want to change initial value of my first for loop. Because I dont want to continue
% from the last i. for example last i is 22, and I want to change it with 50.
% How can I change it in loop. ???
end
end

dpb on 6 Aug 2014
Edited: dpb on 6 Aug 2014
Can't change the loop index variable that controls the loop inside it -- see the following demo--
>> for i=1:4,disp(['before mod: ' num2str(i)]),i=i+pi;disp(['after mod: ' num2str(i)]),end
before mod: 1
after mod: 4.1416
before mod: 2
after mod: 5.1416
before mod: 3
after mod: 6.1416
before mod: 4
after mod: 7.1416
>>
Note that despite futzing with i, when the loop started over the next iteration, Matlab reset it back to the proper next value in the loop and terminated the loop after the four iterations. This is owing to the fact that the loop conditions are evaluated BEFORE the loop begins and stored independently from the local variable referenced inside the loop body.
If you need to have a more flexible looping arrangement that computes some differing looping construct other than a for loop. Several options would be available, one that might be reasonably simple would be to place your for loop in a while something like
whileFlag=true; % logical variable to control continuation
lp1=1; lp2=100; % initial FOR indices
while whileFlag % start the overall loop
for i=lp1:lp2
% do stuff on i here
if condition to change indices % whatever causes the need to change
lp1=i+23;
lp2=222; % the new loop values
break
end
end
if condition to quit entirely % the whole thing is now over
whileFlag=false;
end
end
Another solution is to not use the loop index as the reference variable at all but to compute that variable internal to the loop. This has the advantage of setting that index arbitrarily but then the overall continuation of the loop isn't necessarily going to either keep going long enough or quit when expected.
The simplest solution would really depend on just precisely you needs are which aren't fully developed in the question.

You can use a pre-defined array with a for loop.
e.g.
myArray = 22:100
for i = myArray ... end
You can't change the value of i within a for loop to skip from e.g. 22 upto 50 though all within the same for loop run as far as I am aware
dpb on 6 Aug 2014
Well, you can simulate such, though...which is another alternative (and something similar to which I'm guessing is what OP did given her comment on my earlier Answer) --
myArray = 22:100
for i = myArray
mySkipVal1=37; % some (possibly computed) points at which to skip
mySkipVal2=43;
if iswithin(i,mySkipVal1,mySkipVal2),break,end
...
end
Utility function iswithin is
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);

Ben11 on 6 Aug 2014
Maybe with something like this:
for k = 1:100
if k== 20 % add you condition here
disp('condition violated!')
k = 50
end
end
##### 2 CommentsShowHide 1 older comment
Ben11 on 6 Aug 2014
Oh shoot thanks for pointing that out; the tests I made were not thorough enough I guess.

Fatih on 6 Aug 2014
I have resolved that topic. I have tried other ways but these are not success. dpb's way true. but late answer . :)) Thank you guyss.

### Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by