next up previous
Next: Relational and logical operators Up: MATLAB summary Previous: MATLAB functions and globals

For-loops


The simple MATLAB command
x=0:2:10;
can be coded alternatively using a for-loop as
for jj=1:6,
  x(jj)=2*(jj-1);
end

For-loops can be nested, e.g.
n=5;
for ii=1:n 
   for jj=1:n 
     a(ii,jj)=ii+jj-1; 
   end 
 end

A for-loop that prints out the identity matrices of order n, where n runs from 1 to 4.
n=4;
for jj=1:n
  a=eye(jj)
end



Charlie Macaskill
Fri Mar 5 16:05:28 EST 1999

Copyright © University of Sydney 2003