For Loops

 

for i = 1:6050,
     dx(i)=X(i+1)-X(i);
     dy(i)=Y(i+1)-Y(i);
end;
 
A loop consists of
  • the word "for", a counter variable (also called an index) which must be an integer, and the limits of the loop.  In this case the loop will execute with i from 1 to 6050;
  • a number of statements, which should be indented to help you keep track of the bounds of the loop.  In this case we are filling vectors (or arrays) for dx and dy.  We could do other things, and use IF statements to vary our actions depending on the specific values.
  • we will be computing one value of the array/vector at a time, and we explicitly say which one with the "(i)" notation.  We do not use the "." notation.
  • the word "end" to indicate the end of the loop
     dx(1)=X(2)-X(1);
     dy(1)=Y(2)-Y(1);
 
First run through the loop:
  • i is 1
  • we compute dx(1) and dy(1), using the x and y values in the 1st and 2nd positions
     dx(2)=X(3)-X(2);
     dy(2)=Y(3)-Y(2);
Second run through the loop:
  • i is 2
  • we compute dx(2) and dy(2), using the x and y values in the 2nd and 3rd positions

 

....... .......
    dx(6050)=X(6051)-X(6050);

    dy(6050)=Y(6051)-Y(6050);

 

Last (6050th) run through the loop:
  • i is 6050
  • we compute dx(6050) and dy(6050), using the x and y values in the 6050th and 6051st positions
  • You must be careful, since this array will have 6050 values, but the X and  Y arrays used to compute it have 6051.

 

,

last revision 2/1/2010