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:
for loop problem

Subject: for loop problem

From: Sean Douglas

Date: 6 Aug, 2010 16:02:04

Message: 1 of 2

Hello, I have a for loop that is iterating every other number for some reason. I can’t figure out why. I put an i in the code right after the for loops first line and the output it is showing is saying that this for loop is counting up by 2’s. here’s the code, I think the really important info is the for loop but I will show you as much as I can incase having this for loop work with another for loop and being in a while loop could somehow make it count by 2’s.
xx=0;
yy=0;
p=0;
m=1;
flag=1;
profit=0;
prof=0;
 
while(m+p<=n)
   
    for i =(m+p):n;
          i % to check output
          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
for p = y:n;
         %% CLOSE OUT 1
        if((s(p)/Mc(p)-s2(p)/Mc2(p)<spread2)&&(flag==-1)&&(dagger==1))

    ………………….
 …………………the while loop continues....
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.
Please help

Subject: for loop problem

From: Andy

Date: 6 Aug, 2010 16:13:04

Message: 2 of 2

You were receiving help for this problem in another post:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/288672

Please do not duplicate your posts.

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