Lecture 4Warm up Exercise:
Flow ControlUp to now, we saw how to make Matlab execute a list of commands without any decision making. Now we will learn to have Matlab do different things depending on the results of previous expressions. if StatementWe might want have Matlab evaluate a bunch of expressions if some condition is satisfied, and another bunch of expressions otherwise. The syntax for this is (don't type this into Matlab..) if condition Bunch Of expressions else Other expressions end Note that you must replace the condition with an expression that Matlab will evaluate first. Example: >> if 3<4 '3 is less than 4' else '3 is not less than 4' end >> Since the condition is true, the expression which is evaluated is '3 is less than 4', and this is then displayed on the screen. We can try something more interesting. Type the following into a file (say, if_example.m) x = rand; s = 'The number %g is %s than 0.5\n'; if x<0.5 fprintf(s,x,'less') else fprintf(s,x,'more') end Save the file and run it (either from the menu, or by typing the name of the file). You might be asked to change directory, do that. As you know, rand returned a (pseudo)random number (to initialize it, you need to call something like rand('twister', sum(100*clock)), but read more about rand to find out why.) fprintf is similar to sprintf except that it writes the result instead of returning it. If you want to understand this a little further..read the help files and look at the difference in the output from the following commands:
>> a=sprintf("Hello!") >> b=fprintf("Hello!") The '\n' at the end of the string s is to make sure that Matlab actually goes down a line before returning us the command prompt...try removing it and see what happens.
What is Truth?I'm not trying to get all philosophical or anything, but we need to know what matlab considers to be "true" and what to be "false", if we want to understand how the execution of an if statement works. For this I want you to create another file (oracle.m) with the following if x 'True' else 'False' end Then, from the command line, assign various values to x and run your file. See if each x value is true or false. Things you can try out.
x=(1==4) x=4 x=0 x=[0 0] x=[] x=[0 1] x=[1 2 3] x='hello' x='' x=' ' x=1 x=i for LoopsLoops are good when we want to do stuff over and over again. For example, if we didn't know how to sum up the elements of a vector using sum or linear algebra commands, we could use a loop to do it (a bad idea...much slower than the alternative, about 100 times slower!!! check out loop_timer.m if you care.) However, slow they may be, but we might still need them....The syntax is as follows: for variable = vector a bunch of expressions here variable is an ELEMENT of vector end
The way it works is that for every element in vector, matlab runs through all the commands in the block. during each such execution, the value of variable is set to that element...Let see a simple example: for v = 1:10 v v^2 end
Notice that for every number from 1 to 10 we have two printouts (since there were two commands in the block) one simply the number and the other is the number squared...just as we asked. One can have any legal variable name instead of the variable, but note that it gets overwritten. Also the vector, can be any vector...
for v = 'hello' v end One can "break out" of a loop before it is done by issuing a break command: for v = 'hello and goodbye' v if v=='d' break end end OK, enough new material..lets exercise! Exercises
