Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
• A conditional loop
• “while”
• Used when you do not know ahead of time how many times
some action will be repeated
• Repeat until some logical statement becomes false
• Used with a pre-defined number of iterations of the loop variable
The “for” keyword (“for” is a reserved word in MATLAB)
The loop variable, which can be any valid variable name
Traditionally, we use i, j, k, or other single letters
The range or list of values the loopVar will take on
Can be any vector, but typically the colon operator is used
The action(s) that will be performed during each iteration
The “end” keyword. Signifies that the code should go back to
beginning of the loop statement if more iterations remain
• After loop is
complete
• Can still use the
loop variable
• Has the final value
of the loop
• The loop iterations can be
defined by any vector
• This is not typical
• Whenever possible, use the
colon operator
A common use
of a loop:
• perform an
operation on each
and every data
point, one by one.
• Doesn’t matter how
many data points
there are*
*Obviously, there is some limit based on your computer’s RAM, CPU speed, and operating system
• Lets mimic the
behavior of the
MATLAB function
“sum”
• Use a for loop
Because MATLAB code is interpreted on the fly
(i.e. not compiled into binary exe files)
• Each time the loop restarts, the whole loop must be
compiled again
• To avoid using loops (vectorization)
• For most operations, loops are still fast enough
• To calculate execution times of scripts, MATLAB provides
a timer function
• “tic” starts the timer
• “toc” stops the timer and prints the total execution time to the
command window
• Makes most sense to use in scripts/functions
• Using tic toc, we can determine if code is efficient
Each time the
loop iterates...
• velKmHr is
completely re-
allocated and
remade each
iteration
• This is why the
editor gives a
warning
• orange wiggly
line
Each time the
loop iterates...
• One entry in
velKmHr is
overwritten
• The whole
matrix doesn’t
need to be
remade
• Numerical results are
identical
• Second script pre-
allocates the velKmHr
variable
• Runs > 5x faster!!!
• A loop to find the maximum of a vector
• We can combine loops with Boolean logic to test if each
entry meets some criterion
• We can combine loops with Boolean logic tests
• We can nest “for” loops into inner and outer
loops
• Can also be done with “while” loops
• Often useful for dealing with 2D data
while condition
action(s)
end
Tests if x < 8
• If true:
• Prints out x to command
window
• If false:
• Loop terminates
• Can test more than
one condition in a
while loop
• Tests if x < 8 or y < 33
• Make sure the
condition tested will
become false
eventually
• If condition tested is
never false:
• An infinite loop
• Kill by typing ctrl+c
• May take several
tries
• What should be
added?
• If you use a while
loop to read though
a matrix/vector
• The whole
matrix/vector may
not be read
• Sometimes this is
desired