------------------------------------------------------------------------------- Help on FOR from MATLAB The following MATERIAL is copied DIRECTLY from MATLAB is completely the property of MATHWORKS. I have given links to where you will find the following help. ------------------------------------------------------------------------------- >> help for FOR Repeat statements a specific number of times. The general form of a FOR statement is: FOR variable = expr, statement, ..., statement END The columns of the expression are stored one at a time in the variable and then the following statements, up to the END, are executed. The expression is often of the form X:Y, in which case its columns are simply scalars. Some examples (assume N has already been assigned a value). FOR I = 1:N, FOR J = 1:N, A(I,J) = 1/(I+J-1); END END FOR S = 1.0: -0.1: 0.0, END steps S with increments of -0.1 FOR E = EYE(N), ... END sets E to the unit N-vectors. Long loops are more memory efficient when the colon expression appears in the FOR statement since the index vector is never created. The BREAK statement can be used to terminate the loop prematurely. See also IF, WHILE, SWITCH, BREAK, END. ------------------------------------------------------------------------------- From MATLAB->Getting Started->Programming with MATLAB->for for The for loop repeats a group of statements a fixed, predetermined number of times. A matching end delineates the statements. for n = 3:32 r(n) = rank(magic(n)); end r The semicolon terminating the inner statement suppresses repeated printing, and the r after the loop displays the final result. It is a good idea to indent the loops for readability, especially when they are nested. for i = 1:m for j = 1:n H(i,j) = 1/(i+j); end end ------------------------------------------------------------------------------- From MATLAB->Using MATLAB->M-File Programming-> Programming and Data Types: M-File Programming: for for The for loop executes a statement or group of statements a predetermined number of times. Its syntax is: for index = start:increment:end statements end The default increment is 1. You can specify any increment, including a negative one. For positive indices, execution terminates when the value of the index exceeds the end value; for negative increments, it terminates when the index is less than the end value. For example, this loop executes five times. for i = 2:6 x(i) = 2*x(i-1); end You can nest multiple for loops. for i = 1:m for j = 1:n A(i,j) = 1/(i + j - 1); end end Note You can often speed up the execution of MATLAB code by replacing for and while loops with vectorized code. See "Vectorization of Loops" on page 1-91 for details. Using Arrays as Indices The index of a for loop can be an array. For example, consider an m-by-n array A. The statement for i = A statements end sets i equal to the vector A(:,k). For the first loop iteration, k is equal to 1; for the second k is equal to 2, and so on until k equals n. That is, the loop iterates for a number of times equal to the number of columns in A. For each iteration, i is a vector containing one of the columns of A. ------------------------------------------------------------------------------- MATLAB Function Reference: for for Repeat statements a specific number of times Syntax for variable = expression statements end Description The general format is for variable = expression statement ... statement end The columns of the expression are stored one at a time in the variable while the following statements, up to the end, are executed. In practice, the expression is almost always of the form scalar : scalar, in which case its columns are simply scalars. The scope of the for statement is always terminated with a matching end. Examples Assume n has already been assigned a value. Create the Hilbert matrix, using zeros to preallocate the matrix to conserve memory: a = zeros(n,n) % Preallocate matrix for i = 1:n for j = 1:n a(i,j) = 1/(i+j -1); end end Step s with increments of -0.1 for s = 1.0: -0.1: 0.0,..., end Successively set e to the unit n-vectors: for e = eye(n),..., end The line for V = A,..., end has the same effect as for j = 1:n, V = A(:,j);..., end except j is also set here. See Also break, end, if, return, switch, while The colon operator : -------------------------------------------------------------------------------