Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
why is for loop iterating by 2's ?

Subject: why is for loop iterating by 2's ?

From: Sean Douglas

Date: 6 Aug, 2010 07:28:06

Message: 1 of 12

Hello all, I have a for loop that seems to be counting by 2’s, I can not figure out why. I have some of the code below, I don’t think the while loop has anything to do with the problem I am having, but I will leave the beginning of it in the code I show you, just in case it is.

As I said, the problem is that this for loop is counting by 2’s, (I added an i in the code right between the beginning line of the for loop and the beginning line of the if statement to keep track of the i's in the output and I can see that the i’s are jumping up by 2 on each iteration.

xx=0;
yy=0;
p=0;
m=1;
flag=1;
profit=0;
prof=0;
 
while(m+p<=n)
   
    for i =(m+p):n;
         
          if((s(i)/Mc(i)-s2(i)/Mc2(i)>spread1)&&(flag==1))
           sharesS2=s(i)/s2(i);
           buy= s2(i) ;
           buys2plot(end+1)=s2(i); axbuys2plot(end+1)=i;
           spentbuyS2=sharesS2*buy;
           short=s(i); shorts1plot(end+1)=s(i); axshorts1plot(end+1)=i;
           sharesS1=1;
           date1=t(i); datevector1open(end+1)=t(i); datevectorindexo1=i;
           portvalue(end+1)=-s(i)+sharesS2*s2(i);
           xx=1; flag=-1; dagger=1;
         
           
          elseif((s2(i)/Mc2(i)-s(i)/Mc(i)>spread1)&&(flag==1))
          
           sharesS2=s(i)/s2(i) ;
           buy=s(i);
           buys1plot(end+1)=s(i); axbuys1plot(end+1)=i;
           sharesS2vec1(end+1)=sharesS2;
           short=s2(i); shorts2plot(end+1)=s2(i); axshorts2plot(end+1)=i;
           spentshortS2=sharesS2*short;
           date2=t(i); datevector(end+1)=t(i); datevectorindexo2=i;
           
           xx=1; flag=-1; dagger=2;
         
          end
              if(xx==1)
                  break, %shoots out of next end( for i = m+p:n)
              end
        
    end
    
       
    y=i+1
    
 …………………the while loop continues....

Please help

Subject: why is for loop iterating by 2's ?

From: Darren Rowland

Date: 6 Aug, 2010 07:44:05

Message: 2 of 12

Hi Sean,

Try setting a breakpoint and step through the program while it's executing. This way you can keep track of all variables.

Hth
Darren

Subject: why is for loop iterating by 2's ?

From: Jan Simon

Date: 6 Aug, 2010 13:00:24

Message: 3 of 12

Dear Sean,

> As I said, the problem is that this for loop is counting by 2’s, (I added an i in the code right between the beginning line of the for loop and the beginning line of the if statement to keep track of the i's in the output and I can see that the i’s are jumping up by 2 on each iteration.

I cannot find the "added i" in your code.
The FOR loop you have posted increments i by 1.


> for i =(m+p):n
> % Insert this: disp(i); !!!
> ... The intermediate code cannot change the loop counter i
> end
> y = i+1

I'd never access the loop counter i outside the loop. In former Matlab versions the value of the counters was not well defined.
Do you expect "i" to equal "n" after the loop? Then I'd use "n".

But of course, the former suggestion of using the debugger hits the point. While the debugger has full access to the variables, values and functions, this newsgroup has just some poor implementations of cristal balls.

Kind regards, Jan

Subject: why is for loop iterating by 2's ?

From: Sean Douglas

Date: 6 Aug, 2010 17:34:04

Message: 4 of 12

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i3h118$3cv$1@fred.mathworks.com>...
> Dear Sean,
>
> > As I said, the problem is that this for loop is counting by 2’s, (I added an i in the code right between the beginning line of the for loop and the beginning line of the if statement to keep track of the i's in the output and I can see that the i’s are jumping up by 2 on each iteration.
>
> I cannot find the "added i" in your code.
> The FOR loop you have posted increments i by 1.
>
>
> > for i =(m+p):n
> > % Insert this: disp(i); !!!
> > ... The intermediate code cannot change the loop counter i
> > end
> > y = i+1
>
> I'd never access the loop counter i outside the loop. In former Matlab versions the value of the counters was not well defined.
> Do you expect "i" to equal "n" after the loop? Then I'd use "n".
>
> But of course, the former suggestion of using the debugger hits the point. While the debugger has full access to the variables, values and functions, this newsgroup has just some poor implementations of cristal balls.
>
> Kind regards, Jan

hey thanks all,

can anyone explain what jan means by "using n", does he mean in replace of i? i dont expect i to equal n until the very last iteration.
i did some more tests on this code I have posted and it is indeed incrementing by 2.The output is showing that my indexing i’s in this for loop are all even numbers so my program is skipping the evaluation of every other data point.
I am trying to solve with the debugger. I can not wait to solve this one.... any ideas?

thanks for any help

Subject: why is for loop iterating by 2's ?

From: Andy

Date: 6 Aug, 2010 18:01:06

Message: 5 of 12

"can anyone explain what jan means by "using n", does he mean in replace of i? i dont expect i to equal n until the very last iteration."

Jan's referring to this part of your code:

    for i =(m+p):n;
        % snip
    end
    y=i+1

The comment was that you should not try to access the loop counter i outside of the loop. It is NOT the case, however, that y will necessarily get set to n here, because of the break. Still, it would be clearer if you did this:

for i=(m+p):n
    % stuff
    if (x==1) || (i==n)
       y=i; % set y before you break
       break
    end
end

"I am trying to solve with the debugger. I can not wait to solve this one.... any ideas?"

Well, what is the debugger telling you? You should be stepping line by line through the for loop, and you should be able to see the instant i changes in a way that it shouldn't. That should tell what line is the problem, and you should post that information here. We cannot run your code, as there are pieces of data you haven't given to us.

Subject: why is for loop iterating by 2's ?

From: Matt Fig

Date: 6 Aug, 2010 18:03:06

Message: 6 of 12

Is there any way you could provide a copy-and-paste version of you code (a good and standard practice by the way), because I cannot duplicate your issue because of variables I don't have access to. Pare it down and you may get more help.

Subject: why is for loop iterating by 2's ?

From: Jan Simon

Date: 6 Aug, 2010 18:26:04

Message: 7 of 12

Dear Andy,

> Jan's referring to this part of your code:
> for i =(m+p):n;
> % snip
> end
> y = i+1
> The comment was that you should not try to access the loop counter i outside of the loop. It is NOT the case, however, that y will necessarily get set to n here, because of the break.

Thanks! I've overlooked the BREAK. Jan

Subject: why is for loop iterating by 2's ?

From: Jan Simon

Date: 6 Aug, 2010 18:59:05

Message: 8 of 12

Dear Sean,

> while(m+p<=n)
> for i =(m+p):n;
> disp(i);
> end
> end

I hope the debugger showed you what's going on already.

What about this theory:
Looking at these lines, it is clear, that the FOR loop is processed in steps of 1. But you break the FOR loop in the first iteration and then obviously m or p are increased by 2 (or both by 1).

Jan

Subject: why is for loop iterating by 2's ?

From: Sean Douglas

Date: 6 Aug, 2010 20:49:04

Message: 9 of 12

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i3hm1p$30c$1@fred.mathworks.com>...
> Dear Sean,
>
> > while(m+p<=n)
> > for i =(m+p):n;
> > disp(i);
> > end
> > end
>
> I hope the debugger showed you what's going on already.
>
> What about this theory:
> Looking at these lines, it is clear, that the FOR loop is processed in steps of 1. But you break the FOR loop in the first iteration and then obviously m or p are increased by 2 (or both by 1).
>
> Jan

Hey guys,
Jan i found something that makes me think it is not what you suggested in your last post....When I made a simpler version of my code to post, the problem went away, of course right. So that part of the code seems ok becuase i used that same method. I have now figured out it has something do with the spread1 variable, at least very related to it. This Spread1 variable is what helps decide if the 'if statement' is met here is the line with the Spread1 variable from my ealier post.
 if((s(i)/Mc(i)-s2(i)/Mc2(i)>spread1)&&(flag==1))

if I make the spread 1 variable large enough to that the if statment is never met the i's iterate correclty, however if I make the spread1 variable less than about 1 where the if statment condition is met, the 'for loop' iterates every other one still.

Ofcourse in my simpler code it works no matter what i make spread1 equal to, the simple code works...
any suggestions as to why my actual code would iterate correctly when the conditions are never met or when spread1 is set greater than 1? or why it iterates every other i once a the condition is met?
 

Subject: why is for loop iterating by 2's ?

From: Jan Simon

Date: 6 Aug, 2010 21:09:05

Message: 10 of 12

Dear Sean,

what did the debugger tell you ?!

If you use an offically released Matlab version and your computer is not damaged or magic, the FOR loop will *not* increase i by 2.

 while(m+p<=n)
     disp('*** I start the FOR loop now');
     for i =(m+p):n;
          fprintf('== i is %g\n', i)
           if((s(i)/Mc(i)-s2(i)/Mc2(i)>spread1)&&(flag==1))
             ...
           end
     end % FOR i
 end % while m+p <= n

Please take the time to insert at least these two lines to display, when the FOR loop is started and when i is increased in the inner FOR loop. Matlab is for sure exactly doing, what you have commanded:
  for i = (m+p):n
increases i by 1 in every *performed* iteration.

Kind regards, have fun with the debugger, Jan

Subject: why is for loop iterating by 2's ?

From: Sean Douglas

Date: 6 Aug, 2010 21:52:03

Message: 11 of 12

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i3htlh$bf2$1@fred.mathworks.com>...
> Dear Sean,
>
> what did the debugger tell you ?!
>
> If you use an offically released Matlab version and your computer is not damaged or magic, the FOR loop will *not* increase i by 2.
>
> while(m+p<=n)
> disp('*** I start the FOR loop now');
> for i =(m+p):n;
> fprintf('== i is %g\n', i)
> if((s(i)/Mc(i)-s2(i)/Mc2(i)>spread1)&&(flag==1))
> ...
> end
> end % FOR i
> end % while m+p <= n
>
> Please take the time to insert at least these two lines to display, when the FOR loop is started and when i is increased in the inner FOR loop. Matlab is for sure exactly doing, what you have commanded:
> for i = (m+p):n
> increases i by 1 in every *performed* iteration.
>
> Kind regards, have fun with the debugger, Jan

Dear, Jan
I'm working on the debugger , ive been wanting to figure out how to use the debugger, i guess now is a great time to do it,

Subject: why is for loop iterating by 2's ?

From: Andy

Date: 7 Aug, 2010 01:50:08

Message: 12 of 12

> Dear, Jan
> I'm working on the debugger , ive been wanting to figure out how to use the debugger, i guess now is a great time to do it,

To use the debugger, enter a break point right before the while loop (if you're editing your file with the built in editor, click on the little horizontal line to the left of the line, and it will turn into a red dot. This means you've set a break point there. Click it again when you want to get rid of it.)

Now run your code. When the execution of the program reaches the break point, it will stop, giving you control. The variables created by your function will appear in the workspace and you can see their values. You can press (I think, but check first) F10 to advance 1 line at a time. After each line, you'll have the opportunity to see the values of any variables you want.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us