0:00
[MUSIC]
We've been doing comparisons in the conditions of if statement with
expressions like x equals equals 2.
The double equal is an example of a so-called relational operator.
Relational means the result depends on the relation between the operands.
This table lists all six MATLAB's relational operators.
We seen how to use these operators inside if statements, what maybe surprising
is that they can also be used outside if statements and they produce values.
Let's take a look at this other way to use relational operators.
Let's see.
10 double equals 20,
answer equals 0.
3 double equals 35 minus
32, answer equals 1.
In the first example, we asked MATLAB to calculate the value of
the expression 10 double equals 20 and it told us that the value's 0.
In MATLAB, when the operator double equals finds that its first operand,
like 10 is not equal to its second operand, like 20.
It returns the value zero, which means false.
In the second example, we found that the value of the expression
3 double equals 35 minus 32 is 1,
because 35 minus 32 is 3 and so 3 double equals 3 is true.
And when the double equals operator finds that its first operand is equal
to its second operand, it returns the value one, which means true.
In fact, every relational operator returns zero when its expression is false and
one when its expression is true.
And when we say, zero and one, we mean the numbers zero and one.
Here's an example to drive home
the fact that the result for
relational operation is a number.
In this expression, 16 times 64 ,which
evaluates to 1,024 is greater than 1,000.
So the relational expression in parenthesis evaluates to one,
which is true of course.
We then add 9 to 1 and get 10.
So the value of 16 times 64 greater than a 1, 000 is a 1, a normal 1.
The kind of one that you can do arithmetic with not some special one that
has some special sort of truthiness attribute attached to it.
The parentheses have an important effect here.
If we omit them, we get a different answer.
3:12
This time, the addition operator is executed before the greater than operator.
That happens,
because the precedence of plus is higher than the precedence of greater than.
So this operation is carried out first.
In fact, all the arithmetic operators have a precedence than all
the relational operators, we'll talk more about this later in this lesson.
So the result of the addition is 1,009, but
the value of the operand on the left of the greater than sign,
which as before is 1,024 is still greater than
the value of the operand over here on the right, 1,009.
So the relational operator produces the value one, which means true as before.
Since nothing is added this time, the result of the entire expression is one.
Let's take a look at this little function called if test,
which helps us test what MATLAB considers true and what it considers false.
This function uses an if statement to decide whether x,
the input argument here is true or false.
It's true, it prints it's true.
If it's false, it prints it's false.
Let's try calling with a few different values.
Well, zero is false and we knew that.
Let's try it with ten.
Ten is true, it's non-zero.
What about minus 1?
It's non-zero, but it is negative.
Now, it's non-zero, so it's true.
And one, well, we knew that was true too.
What about this?
5:15
That is a small number.
I wonder if it's big enough to be true.
It's awful close to zero, true.
You can easily see the pattern.
If it's zero, MATLAB takes it as false.
For anything else be a negative integer or not big or small.
MATLAB takes it as true.
5:52
But we're not quite done with relational operation,
there's one important feature that we need to look at.
Relational operators work on arrays too.
First, let's remember how array operations work.
Let's do array multiplication
on two vectors, like this.
MATLA applies the dot star operation to each corresponding pair of elements.
First, 4 times 5 gives 20.
Here we go.
4 times 5 gives 20.
And minus 1 times minus 8 gives 9.
Minus 1 times minus 9 gives 9 and so forth.
And the resulting matrix is the same size as the input matrices,
which in this case were five element row vectors.
Now to replace the array multiplication operator with the relational operator,
the greater than, like this.
7:07
As with the dot star array operator, MATLAB applies a relational
operation one by one to each corresponding pair of elements.
First, four greater than five gives zero.
So here's the four greater than five gives zero.
Since four is not greater than five and zero represents false.
Then minus 1 greater than minus 9 gives 1.
Since minus 1 Is greater than minus 9 and so forth.
And again, the resulting matrix is the same size as the input matrices,
five element rovector.
As with the arithmetic array operators, the upper ends of the relational
operators must in fact, have the same exact size, except for one special case.
It's the same special case we saw with the arithmetic array operators.
And that's when one of the operands is a scalar, like this.
8:16
In that case,
each element of the array in turn serves as one operand of the relational operator.
While the single scalar serves as the other operand.
And the results, an output array of the same size as the input array.
So, 4, less than or equal, 4 is evaluated, which is true and gives 1.
Then minus 1 is less than or equal 4 is evaluated, that's true, that gives 1.
10:57
If MatLab is asked to consider the input zero as logical,
it will interpret that as false.
And when MatLab outputs illogical faults, it outputs the value zero.
But when MatLab outputs a logical true, it always outputs the value 1.
13:37
Suppose we want a function that takes three inputs and
returns one of the following three values.
A one, if its three inputs are in increasing order, a minus one,
if they are in decreasing order, and a zero, otherwise.
14:27
As you may remember from the truth table, this and
operator returns true only if both of its operand are true.
This is the first operand, x less than or equal to y.
This is the second operand, y less than or equal to z.
So in this case, x has to be less than or equal to y, and, and at the same time,
y needs to be less than or equal to z in order for this condition to be true.
15:26
And if y is equal to z, they're not increasing, either.
But at least we know that they're not decreasing.
So to be more precise we should say that x, y, and z are in non-decreasing order.
And that's a thing, by the way, non-decreasing.
It's the precise mathematical description.
Anyway, if they are non-decreasing, the and operator returns true.
15:52
The variable a is set to 1.
The if else if else statement terminates, and so does the function.
And since the function returns A as you can see up
here then the function returns a 1.
So that's what happens if x is less than or
equal to y, and y is less than or equal to z.
But if either of these relationships is false, then the and
is false and we take the else-if branch.
It's condition is x greater than or equal to y, and y greater than or equal to z.
17:02
In that case, A is set equal to minus 1.
The if else if else statement terminates and so does the function,
and since it returns A, the function returns a minus 1.
Finally, if neither of the two conditions is true.
17:23
This one or this one, we go to the else branch down here.
This is the catch all case.
The numbers are out of order, and for that case, a is set equal to zero,
and the function returns a zero.
18:53
In English, this condition reads, x greater than or
equal to y or x greater than or equal to z.
And that makes sense, because if x is greater than or
equal to y or z, or both for that matter, then it's not the smallest.
In that case, the or operator returns true.
19:14
This statement is executed.
A is assigned the value 1.
And the function returns that 1.
Otherwise, we skip to the else branch.
A is assigned to value 0, and the function returns 0.
20:07
Okay let's pull up our order three function again.
There it is.
And we'll remember that it's supposed to give a one if they're increasing order or
more specifically non decreasing order.
A minus one if they're decreasing or
more specifically non increasing and a zero otherwise.
[NOISE] Well that looks good because they're in non decreasing order.
[SOUND] That's also good non-decreasing.
[SOUND] Whoops.
Picked the wrong function, but MATLAB guessed what I wanted.
So, we'll go with that.
20:51
And that looks good These are decreasing or more precisely,
non increasing.
[NOISE] Also non increasing and let's do one out of order.
[NOISE] I hesitated after that parentheses to give MATLAB enough time
to give me the hint that there are three arguments for this function.
[SOUND].
And the three arguments that I gave it were out of order which means we should
get a zero and we did get a zero.
So it appears that both our little functions are working.
21:31
We've seen how to use logical operators, and, and, or, inside if statements.
Well, they can be used outside of if statements too.
In the same way, as we saw earlier, that relational operators can.
22:52
When either or both of the operands of the or operator are nonzero,
which is equivalent to true, the operator gives true in the form of the number one.
But if both of the operands are zero, which is equivalent to false,
then the operator gives false in the form of the number zero.
23:44
Then when the not operator is applied to that true, it's changed to false.
The operation b and c is false, too,
because c is false, as we can see here.
So the or operator here, is given two operands that are false.
False, and false.
24:20
Let's make one tiny change.
We'll replace the second and operator here by or.
[SOUND] The result is different because b or
c, unlike b and c, gives a true value.
That true value becomes the operand of this first or, which then gives true.
25:01
But as with the relational operators, it's
a bit unexpected to find that they can be mixed with the arithmetic operators.
[SOUND] What happens here is that
the expression in parenthesis is true.
Because b is equal to 2 and 2 falls between 0 and 10.
So the and operator gives 1.
And then b is multiplied by 1 to get b, which equals 2.
Okay, as we said earlier,
when we were mixing arithmetic with relational operators,
this shouldn't be surprising, because one is well, you know, a 1 and 2 times 1 is 2.
25:57
You could initialize your total like this.
[SOUND] And then every time you get a new value for
b, you get at it like this.
[SOUND] Five is greater than zero and
less than ten, so it gets added in.
[SOUND] The same holds for three.
26:48
By the way, the parenthesis are important, because the precedence of and, and
also of or, are lower than the arithmetic operators.
And here we need and to go before multiplication.
In fact, the logical operators are at the bottom of the list.
29:21
You might expect to see a dot show up in the array versions of these operators.
After all, for the arithmetic operations multiply and
divide as you remember, each array version adds a dot to the operator.
But for the array versions of and, and or, there's only one ampersand for
the and operator instead of two.
And only one vertical bar for the or operator, instead of two.
30:22
And lets get a little bit fancier.
[SOUND] Before I hit return,
can you figure out the answer?
Spoiler alert, I'm about to walk you through it.
Okay.
The first relational operator, 1.4 less than the square root 2, will give true,
30:49
since 1.4 is a little bit less than the square root 2.
Inside the vector over here, the first element, which is the expression pi
greater than 3, will be true, since pi is indeed greater than 3.
And the second element, minus 1 greater than 1 is obviously false.
So the elements of the vector are true and false.
Which in the land of MATLAB, is one and zero.
33:08
The expression inside the parentheses that we added to
the right produces the vector 0, 1.
Yet again, the less than, which as we learned earlier is an array operator,
is applied to each one of those elements.
Since 1.4 is not less than 0 or 1,
the result is a couple of falses.
In other words, a couple of 0's.
34:23
This is the table that shows up.
It omits parentheses, but
it's pretty easy to remember that they outrank everything else.
By the way, when you're not sure about precedence,
you should probably use parentheses to force the order that you want,
because it'll improve the readability of your program.
[MUSIC]