Skip to main content
Engineering LibreTexts

8.12: Loops and Control

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    MATLAB has control statements like those in most computer languages. We will only study the for loop here. See the MATLAB manual for details on if and while statements.

    What for loops do is allow a statement or a group of statements to be repeated. For example,

    for i = 1:n,x(i) = 0,end

    assigns the value 0 to the first n elements of the array x. If n is less than 1, the instruction is still valid but nothing will be done. If n is not defined, then the following message will appear:

    ??? Undefined function or variable.
    Symbol in question==>n

    If n contains a real value, the integer part of n is used. If n is a complex number, the integer part of the real part is taken. (This should, however, be avoided.) If x is not declared earlier or is of smaller dimension than n, then extra memory is allocated.

    The command end must follow the command for. If it is not present, MATLAB will wait for remaining statements until you type the command end, and nothing will be executed in the meantime.

    More than one statement may be included in the loop. The statement 1:n is the way to specify all the integer values between 1 and n. A step different than 1 may be specified. For example, the statement for i=1:2:5,x(i)=1,end is equivalent to x(1)=1,x(5)=1. Negative steps are also allowed, as in i=n:-1:1.

    We may use a for loop to draw a circle of radius 1. Type

    ≫ j=sqrt(-1);
    ≫ n=360;
    ≫ for i=1:n,circle(1)=exp(2*j*i*pi/n);end;

    Note how easy it is to plot a curve. But also note how slowly the for loop is executed. MATLAB is not very good at executing things one by one. It prefers, by far, a vector-oriented statement. Using the range specification as in the for loop, it is possible to speed up the process by replacing the explicit for loop by an implicit for loop using the colon, like this:

    ≫ circle = exp((2*j*pi/n)*[1:n]);

    Note how much faster this command is executed. In general, for loops should be avoided as much as possible. For example, the first for loop you typed could have been replaced by the command x=zeros(1,n);, which is much more efficient. The function zeros fills the variable with 0's to the specified size. Similarly, the function ones fills the variable with 1's. The size can also be determined by the size of the argument. If A is a matrix of size m,n, then the command B=ones(a) fills the matrix B with 1's and forces the matrix B to have exactly the same size as the matrix A.

    Avoiding for Loops

    Since for loops are very inefficient in MATLAB (they are computed sequentially, adding several more computations for every loop), it is preferable to use the matrix capabilities of MATLAB to replace for loops and speed up processing time.

    1. Replace the for loop ≫ for i = 1:10, ≫ x(i) = i; ≫ end; with ≫ x = 1:10 to get x = [1 2 3 4 5 6 7 8 9 10]
    2. Replace the for loop ≫ z = something; ≫ for i = 1:10, ≫ x(i) = z*i; ≫ end; with ≫ z = something ≫ x = z*(1:10); to get x = [z 2*z 3*z ... 10*z]
    3. Replace the for loop ≫ z = something; ≫ x(1) = z; ≫ for i = 2:10. ≫ x(i) = z*x(i-1); ≫ end; with ≫ z = something; ≫ x = z.^(1:10); to get x = [z z**2 z**3 ... z**10]
    4. Replace the for loop ≫ for i = 0:2:100, ≫ x(i) = 1.0*exp(j*2*pi*i/100); ≫ end; with ≫ x = 1.0*exp(j*2*pi*(0:2:100)/100); to get x = [exp(0) exp(j*4*pi/100) ... exp(j*200*pi/100)]

    This page titled 8.12: Loops and Control is shared under a CC BY 3.0 license and was authored, remixed, and/or curated by Louis Scharf (OpenStax CNX) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.