31p. Loops _printable_

31p. Loops _printable_ - 1 ©2009 by L Lagerstrom Loops •...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 ©2009 by L. Lagerstrom Loops • For an introduction to the basic concepts, see the associated video clip • The loop structure • The for loop • Rules and comments re. loops • The downside of loops • Timing the speed of code using tic and toc • Speeding things up: preallocating the array • Don't use loops if you don't have to • The while loop • Using a counter variable • Using the break statement • Real-life examples: Update process and signal filtering ©2009 by L. Lagerstrom The Loop Structure In addition to conditional statements, another of the principal types of program control structures is a loop (or "iteration") structure. In a loop the same set of instructions is executed over and over again, usually with slight changes each time in the values being used. There are two basic types of loops in Matlab: "for" loops and "while" loops. A "for" loop executes its set of instructions a certain, preset number of times. For example, it might do the same thing 12 times, or 12 "iterations" of its code. A real-life analogy would be a copy machine that copies, collates, and staples a multiple-page handout. The same procedure is done over and over again for each copy. A "while" loop, on the other hand, does not have a preset number of iterations. Depending on the circumstances, it might execute its code 3 times, or 999 times, or even not at all. A real-life analogy would be a recipe that calls for "salt to taste", where you would add some salt, taste the result, add some more, taste again, etc. It might take one iteration of salting, or three, or whatever to get it right. ©2009 by L. Lagerstrom The for Loop Most programming languages have a "for" loop structure. The way in which Matlab implements it, however, is different from many languages. So if you have previous programming experience, this is one of those times where you may be familiar with the concept, but you need to learn a new way of writing it. (If you don't have previous experience, then you don't have to worry about mixing up the syntax between languages.) The basic form of a "for" loop uses colon notation to specify the number of iterations: for k = 1:10 [Do some instructions here...] end The "k = 1:10" part should be familiar: normally it creates a row vector of values from 1 to 10 at intervals of 1, i.e., [1 2 3 ... 10] and assigns it to the variable k. Here it's a little different, however (next slide). ©2009 by L. Lagerstrom The for Loop, cont. A basic for loop: for k = 1:10 [Do some instructions here...] end In the context of a for loop, the "1:10" part represents the row vector [1 2 3 ... 10], but instead of assigning the row vector to the variable k, Matlab assigns the first value in the vector to k (in this case, the value 1). Then it executes the instructions in the loop....
View Full Document

This note was uploaded on 02/23/2010 for the course ENG 42325 taught by Professor Lagerstrom during the Spring '10 term at UC Davis.

Page1 / 7

31p. Loops _printable_ - 1 ©2009 by L Lagerstrom Loops •...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online