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]

Â