Skip to content Skip to navigation

OpenStax-CNX

You are here: Home » Content » An Introduction to MATLAB: Loops and Control

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Endorsed by Endorsed (What does "Endorsed by" mean?)

This content has been endorsed by the organizations listed. Click each link for a list of all content endorsed by the organization.
  • IEEE-SPS

    This module is included inLens: IEEE Signal Processing Society Lens
    By: IEEE Signal Processing SocietyAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Click the "IEEE-SPS" link to see all content they endorse.

  • College Open Textbooks display tagshide tags

    This module is included inLens: Community College Open Textbook Collaborative
    By: CC Open Textbook CollaborativeAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Comments:

    "Reviewer's Comments: 'I recommend this book as a "required primary textbook." This text attempts to lower the barriers for students that take courses such as Principles of Electrical Engineering, […]"

    Click the "College Open Textbooks" link to see all content they endorse.

    Click the tag icon tag icon to display tags associated with this content.

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • Bookshare

    This module is included inLens: Bookshare's Lens
    By: Bookshare - A Benetech InitiativeAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Comments:

    "Accessible versions of this collection are available at Bookshare. DAISY and BRF provided."

    Click the "Bookshare" link to see all content affiliated with them.

  • NSF Partnership display tagshide tags

    This module is included inLens: NSF Partnership in Signal Processing
    By: Sidney BurrusAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Click the "NSF Partnership" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

  • Featured Content display tagshide tags

    This module is included inLens: Connexions Featured Content
    By: ConnexionsAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Comments:

    "A First Course in Electrical and Computer Engineering provides readers with a comprehensive, introductory look at the world of electrical engineering. It was originally written by Louis Scharf […]"

    Click the "Featured Content" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

Also in these lenses

  • UniqU content

    This module is included inLens: UniqU's lens
    By: UniqU, LLCAs a part of collection: "A First Course in Electrical and Computer Engineering"

    Click the "UniqU content" link to see all content selected in this lens.

  • Lens for Engineering

    This module is included inLens: Lens for Engineering
    By: Sidney Burrus

    Click the "Lens for Engineering" link to see all content selected in this lens.

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.
 

An Introduction to MATLAB: Loops and Control

Module by: Louis Scharf. E-mail the author

Note:

This module is part of the collection, A First Course in Electrical and Computer Engineering. The LaTeX source files for this collection were created using an optical character recognition technology, and because of this process there may be more errors than usual. Please contact us if you discover any errors.

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;
≫plot(circle)

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)]

Content actions

Download module as:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks