For-loop

MATLAB knows two repeating constructs. The first is the for-loop, and the second one is the while-loop.
The for-loop construct offers you the possibility to execute certain pieces of a program several times, while the value of certain variables is changing. A so-called loop variable keeps track of how often the program lines have to be repeated.

The general form of a for-loop is:
for loopvariable = start:step:end
    command lines
end
When step is omitted, a step size of 1 is used by default.

Example 6.1: a simple for-loop
% definition of constant n
n = 10;
% producing an array with n rows
% and 1 column, filled with zeroes
A = zeros(n,1);
% fill this column with values from 1 up to
% n in a FOR loop
for k = 1:n
    A(k,1) = k;
end
A % print the result on the screen.
This loop works as follows: after the for command, the loop variable $ k$ is given the value 1, and the program lines until the end are executed. Thus, A(1,1) is given the value 1. After the end command, MATLAB jumps back to the for line, and the loop variable $ k$ is given the value 2. After this, the program lines are executed again: A(2,1) is given the value 2. This is repeated until the last step, where the loop variable $ k$ is given the value 10. After this, the program continues with the lines after end, where A is printed on the screen.

for k = 1:n means `for $ k$ is 1 up to $ n$ with steps of 1'. You can also use larger steps to go from 1 to $ n$ , or start with another value: for example, for k = 2:2:10 means `for $ k$ is 2 up to 10 with steps of 2' (i.e., 2,4,6,8,10). Of course you are free to choose other loop variables like, for instance, $ a$ and/or $ b$ .

Example 6.2
% initialisation of the variable p
p = 0;
% run loop n times
for k = 1:n
    p = p+1; % increase counter variable p
end
p
In this loop the value of $ p$ is raised by one every time the program goes through the loop. When the program goes through the loop for the first time, the new value of $ p$ becomes equal to the old value of $ p$ (which was 0) plus 1, so $ p$ becomes 1. So this is an assignment statement and not an equation. This kind of assignment construction is convenient to use to keep track of a counter or a summation within a loop construction.

It is also possible to nest one or more for loops. By using this, you can easily fill an $ n \times m$ matrix by going through the matrix element by element:

Example 6.3: Give each element of a matrix a random value between 0 and 1
% definition of the size of the matrix
nr = 4;     % number of rows
nc = 3;     % number of columns
% producing an n x m matrix with nr rows
% and nc columns, which is filled with zeroes.
% it is not really necessary to make the matrix
% beforehand and fill it with zeroes, but it is
% neater, it prevents errors, and sometimes it
% may be useful.
A = zeros(nr,nc);
% fill the matrix with random values between 0 and 1
% in a nested double FOR loop
for r = 1:nr
    for c = 1:nc
        A(r,c) = rand(1);
    end
end
A



Previous      Next      Up      Contents


Esteur 2010-03-22