A `for` loop in MATLAB is similar to a DO Loop in FORTRAN. The main difference is that the FORTRAN DO loop must have an integer index variable; `for` does not have this restriction. An example of a `for` loop that is virtually identical to a DO loop is

» for k = 1:5001; t(k) = (k-1)*0.01; y(k) = sin(t(k)); end

Another way of implementing the same loop is to increment t from 0 to 50 in intervals of 0.01:

» k = 0 » for t = 0:0.01:50; k = k + 1; y(k) = sin(t); end

The developers of MATLAB highly recommend that you use the vectorized version of the above `for` loops:

t = 0:0.01:50; y = sin(t);

since the computation time for this method is over 200 times faster than the nonvectorized methods.

Start Free Trial

No credit card required