Conditionals
Logicals
MATLAB
has a class (data type) logical
. Logical variables are scalars or arrays of logical 1
s and 0
s, representing true and false, respectively (in fact, there are “shorthand” variables defined, by default, true
and false
).
Boolean logic expressions evaluate to either true
or false
.
Relational operators
Logicals are returned when an expression containing a relational operator is evaluated. Here are MATLAB
’s relational operators:
- less than:
<
, - greater than:
>
, - less than or equal to:
<=
, - greater than or equal to:
>=
, - equal to:
==
, and - not equal to:
~=
.
Exercises
Exercise 1: comparing expressions
Write a script that considers the following statements and returns logical expressions, which should be displayed using the disp
command. Assign each logical
result to a variable.
- Is
44^2
equal to1936
? - Is
4^6
greater than or equal to6^4
? - Is
5^4
less than5*9*6
?
After running your script, you should have a column of logical 0
s and 1
s.
Exercise 2: exploring the logical
class
Run the script from Exercise 1. The Workspace
should have the variables to which you assigned the logical
s. In the command window, query the data type of one or more of these. Which Class
is displayed? I know.
Introducing boolean logic and algebra
Boolean logic and boolean algebra deal with the following five basic logical operations: $\land$ (and), $\lor$ (or), $\neg$ (not), $\implies$ (if…then…), and $\iff$ (if and only if). All but the last of these can be expressed in most programming languages.
The operators and
, or
, and not
The $\land$, $\lor$, and $\neg$ operators are expressed in MATLAB
in the following manners:
- the $\land$ (and) operator is
&
,&&
, orand(expression_1,expression_2)
; - the $\lor$ (or) operator is
|
,||
, oror(expression_1,expression_2)
; and - the $\neg$ (not) operator is
~
ornot(expression)
.
The single and
and or
operators &
and |
compare element-wise, whereas the double and
and or
operators &&
and ||
assume you are comparing scalar logicals, and are therefore more efficient to use in these cases.
Exercises
Write a script that answers the following questions.
- Is it true that both $37>3^3$ and $\sqrt{9}=4$?
- Is it true that either $37 > 3^3$ or $\sqrt{9} = 4$?
- Is it true that both $8^6 \ne 6^8$ and $4^3 = \sqrt{1844}$?
The operator if
, then, etc.
Procedural logic can be expressed in programs with the if
conditional statement. The structure of these statements is as follows.
if LOGICAL-EXPRESSION
STATEMENTS
elseif LOGICAL-EXPRESSION
STATEMENTS
else
STATEMENTS
end
The statements under each if
and elseif
(of which there can be multiple), are only evaluated if the corresponding logical expression returns true
. Otherwise, the statements are skipped. If no if
or elseif
logical expressions evaluate to true
, the else
statements are evaluated.
It is not necessary to include the elseif
and else
statements. For instance, it is perfectly valid to do the following.
x = rand(1);
if x > 0.5
disp('glass is more than half-full, dude')
end
A transgressive exercise
Apparently, we like to gamble. Let’s play. Write a script that generates three random integers between 1
and 3
, let’s call them x1
, x2
, and x3
. If none of them are equal—game over. If two of them are equal, we are allowed to try again (re-run the program). If all of them are equal, we win the game.