Warm up Exercise:
Up 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.
We 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..)
Note that you must replace the condition with an expression that Matlab will evaluate first.
>> if 3<4
'3 is less than 4'
'3 is not less than 4'
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';
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:
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
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 2 3]
Loops 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
here variable is an
ELEMENT of vector
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
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'
One can "break out" of a loop before it is done by issuing a break command:
for v = 'hello and goodbye'
OK, enough new material..lets exercise!