for - Execute block of code specified number of times

Syntax

for x=initval:endval, statements, end
for x=initval:stepval:endval, statements, end

Description

for x=initval:endval, statements, end repeatedly executes one or more MATLAB® statements in a loop. Loop counter variable x is initialized to value initval at the start of the first pass through the loop, and automatically increments by 1 each time through the loop. The program makes repeated passes through statements until either x has incremented to the value endval, or MATLAB encounters a break, or return instruction, thus forcing an immediately exit of the loop. If MATLAB encounters a continue statement in the loop code, it immediately exits the current pass at the location of the continue statement, skipping any remaining code in that pass, and begins another pass at the start of the loop statements with the value of the loop counter incremented by 1.

The values initval and endval must be real numbers or arrays of real numbers, or can also be calls to functions that return the same. The value assigned to x is often used in the code within the loop, however it is recommended that you do not assign to x in the loop code.

for x=initval:stepval:endval, statements, end is the same as the above syntax, except that loop counter x is incremented (or decremented when stepval is negative) by the value stepval on each iteration through the loop. The value stepval must be a real number or can also be a call to a function that returns a real number.

The general format is

for variable = initval:endval
    statement
    ...
    statement
end

The scope of the for statement is always terminated with a matching end.

See Program Control Statements in the MATLAB Programming Fundamentals documentation for more information on controlling the flow of your program code.

Remarks

It is recommended that you do not assign to the loop control variable while in the body of a loop. If you do assign to a variable that has the same name as the loop control variable (see k in the example below), then the value of that variable alternates between the value assigned by the for statement at the start of each loop iteration and the value explicitly assigned to it in the loop code:

for k=1:2
   disp(sprintf('   At the start of the loop, k = %d', k))
   k = 10;
   disp(sprintf('   Following the assignment, k = %d\n', k))
end

   At the start of the loop, k = 1
   Following the assignment, k = 10

   At the start of the loop, k = 2
   Following the assignment, k = 10

Examples

Assume k has already been assigned a value. Create the Hilbert matrix, using zeros to preallocate the matrix to conserve memory:

a = zeros(k,k)  % Preallocate matrix
for m = 1:k
    for n = 1:k
        a(m,n) = 1/(m+n -1);
    end
end

Step s with increments of -0.1:

for s = 1.0: -0.1: 0.0,..., end

Step s with values 1, 5, 8, and 17:

for s = [1,5,8,17], ..., 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 k = 1:n, V = A(:,k); ..., end

except k is also set here.

See Also

end, while, break, continue, parfor, return, if, switch, colon

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS