What IF?

As you progress toward a better understanding of Mathematical commands in Matlab, I am sure you have realized there are some cases where you want to adjust the computation being done depending on the actual data.

This is when control flow statements become handy.

For instance, let’s suppose you like to flip coins to make important decisions in your life (why not?). In this modern world of absolute domination of credit cards, you might not have a coin. So why not use Matlab? (I know it is not very romantic, but at least it is original and it is certainly better than flipping a credit card…).

Luckily, Matlab can generate random numbers. So you can use the following code.

X=rand;
if X<0.5
   msgbox('Heads');
else
   msgbox('Tails');
end

What this does is to first generate a random number between 0 and 1 using rand. Then you see the appearance of ifelseend. These, as I said, are control flow statements. This means that Matlab will choose the lines it executes depending on the results of the operation X>0.5 . IF X is greater than 0.5, msgbox(“Heads”) is executed (msgbox is just a function to display a text on a dialog box), IF NOT then msgbox(“Tails”) is executed. Matlab uses the END to clearly separate the lines of codes that are under the control of the IF statements. If you have followed my line of thought, then you must have realized that, in this particular example, the outcome of this code will be different at each execution (because of the call to rand).

You can use any operation after IF as long the outcome is logical (0 or 1) and only one number is generated.

IF is a conditional control. There are several conditional controls : IF, ELSE, ELSEIF and SWITCH.

ELSEIF can be used to make a 3 (or more) sided coin, in case your decision is a little bit more complicated like :

X=rand;
if X<1/3
   msgbox('I love him');
elseif X>=1/3 & X<2/3
   msgbox('I do not love him');
elseif X>=2/3
   msgbox('I am not sure');
end

Please note how I used the AND operator & to combine two logical values.

Now, let’s suppose you are a very beautiful girl and you have a lot of choices (even if you are a total nerd playing with Matlab). In that case, SWITCH is handy as it lets you avoid using too many ELSEIF statements to make the decision. Just use :

X=randi(5);
switch X
case 1
   msgbox('I love John');
case 2
   msgbox('I love Max');
case 3
   msgbox('I love Brandon');
case 4
   msgbox('I love Rebecca');
case 5
   msgbox('I love myself');
end

randi is similar to rand, except that it generates an integer between 1 and the given value (here 5).
As you can see, SWITCH indicates that you compare a variable to various values that are given with CASE.
In my next post in this series, we will go over another form of control flow : for loops.

This entry was posted in Beginners. Bookmark the permalink.

3 Responses to What IF?

  1. Pingback: MatLab Basic 2 | New ThinKing

  2. Louis-Math says:

    I think you did a small mistake in your first code segment. According to your text, it shoud be:
    ” if X>0.5 msgbox(‘Heads’); ” … not ” if X<0.5 msgbox('Heads'); " .

  3. Miguel Angel Hernandez Espinoza says:

    Hi, could you please tell me how I can stop the profile and run my code without seeing this window appears each time I try to run. I do not know if accidentally I pressed some keys to activate it. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *