<< Chapter < Page Chapter >> Page >
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 theloop here. See the MATLAB manual for details onandstatements. while

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

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

Ifcontains a real value, the integer part ofis used. Ifis a complex number, the integer part of the real part is taken. (This should, however, beavoided.) Ifis not declared earlier or is of smaller dimension than, then extra memory is allocated. n

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

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

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

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. Itprefers, by far, a vector-oriented statement. Using the range specification as in theloop, it is possible to speed up the process by replacing the explicitloop by an implicitloop using the colon, like this: for

Note how much faster this command is executed. In general,loops should be avoided as much as possible. For example, the firstloop you typed could have been replaced by the command, which is much more efficient. The functionfills the variable with 0's to the specified size. Similarly, the functionfills the variable with 1's. The size can also be determined by the size of the argument. Ifis a matrix of size, then the commandfills the matrixwith 1's and forces the matrixto have exactly the same size as the matrix. A

AvoidingLoops. for Sinceloops are very inefficient in MATLAB (they are computed sequentially, adding several more computations for everyloop), it is preferable to use the matrix capabilities of MATLAB to replaceloops and speed up processing time. for

  1. Replace the for loop

    with

    to get
    x = [1 2 3 4 5 6 7 8 9 10]
  2. Replace theloop

    with

    to get
    x = [z 2*z 3*z ... 10*z]
  3. Replace theloop

    with

    to get
    x = [z z**2 z**3 ... z**10]
  4. Replace theloop

    with

    to get
    x = [exp(0) exp(j*4*pi/100) ... exp(j*200*pi/100)]

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, A first course in electrical and computer engineering. OpenStax CNX. Sep 14, 2009 Download for free at http://cnx.org/content/col10685/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'A first course in electrical and computer engineering' conversation and receive update notifications?

Ask