Lesson 5.3: Relational and Logical Operators

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[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. 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? 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. Okay. We're done with our if test function. No matter, we don't need this stuff anymore. 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. 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. 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. Then 7 is less than or equal to 4, which is false, gives 0, and so on. And the result is an output array that's the same size as the input array. And finally, look at this tricky one. Can you see what we're doing here? We're counting the elements in the vector that are equal to 14. As you can see, there are two of them, here and here. And we got the right answer, 2. Can you see how this works? First, the argument of the sum is evaluated. That's this thing in parenthesis here. It's an expression involving a relational operator, double equals. It has its input, a vector and a scalar. As we've just seen, the relational operator compares each element of the vector in turn to the scalar. Since the relational operator is a double equals, we'll get a one for each element, that is equal to 14 and a zero everywhere else. Since there are two of them equal to 14, we get two ones and four zeroes as our output. Now, you don't see that vector, but you know it's there. That vector of two ones and four zeroes is then sent to sum as its input argument. Sum adds them up and gets 2. And the overall effect is to count the elements of the vector that are equal to 14. Cool, huh? This is a great example of Matlab programming, a single line of code that does something that would take five or six lines in most programming languages. And if you're a Matlab programmer, this version is clearer too. Lets recap the subject of logical values because it's a really important concept. If Matlab is asked to consider a non zero input as logical, it'll interpret it as true. 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. Note that this means that you can give any value to Matlab to be interpreted as logical. If you give it a value that's neither zero nor one, say 98.6 or minus 30, that's fine. Matlab will treat it as true, but it will always return one for true and zero for false. You may have thought about whether we can combine multiple logical values somehow. Yes, we can, using logical operators. The table here shows three logical operators. These are the operators that Matlab has, and, or, and not. Two ampersands mean and. Two vertical bars mean or. And the single tilde means not. So how do they work? Not is a unary operator, meaning that it takes only a single operand. It simply flips the value given to it. If you give it a true, it'll return false. If you give it a false, it'll return true. And and or are binary operators. They combine the value of two operands. The result of and is true if and only if both of its operands are true. Otherwise, it's false. Or works the opposite way. The result of or is false, if and only if, both of its operands are false. Otherwise, it's true. This table is called a truth table. It specifies how logical operators work in a bit more formal manner. The four rows show all possible combinations of input values. And there are two columns of outputs, one for and and one for or, over there on the right. They show the result of the operator for each input combination. And here's a version showing the inputs from the standpoint of Matlab, where zero means fault and non zero means true. As you can see, there's a 1, which means true, in the and column, only when both inputs are non-zero. And there's a 0 for false in the or column, only when both inputs are 0. Now let's see some examples. 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. Well, we've already written a function that does that, called order3, over here. Let's have a look at it. It uses an if, else if, else statement with a logical and operator, here and here. And here's how it works. The first condition in the if else if statement says, x less than or equal to y, ampersand ampersand, y less than or equal to z. This double ampersand is the logical and operator. 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. Notice how I use the word and to specify what's happening here in English. It's not an accident that this logical operator is called and. If you think about it, these two conditions together mean that x, y, and z are in increasing order. Well, if you think about it a little more, though, that's not precisely correct, because if x is equal to y, then those two numbers aren't increasing. 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. 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. This condition is very similar to the first condition the only difference is that the relations are reversed here. We still use the and operator. In this case, in order to get true, x has to be greater than or equal to y, and y at the same time has to be greater than or equal to z. If both of these relationships are true, the and operation will return true and we know that the three numbers are in nonincreasing order. 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. 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. Okay, we've seen an example for the and operator. Now lets see one for the or operator. Not smallest function. A job of not smallest, is to determine whether it's first input argument is smaller than both of it's other two input arguments. It it's not smaller than both of them, than it returns one, meaning it's not the smallest. Hence, the name of the function. Otherwise, it returns 0. To do this, it uses an if l statement, whose condition uses the or operator, which is symbolized by this double vertical bar here. As you may remember from the truth table, the or operator returns false only if both its operands, are false. To put that another way, it returns true if and only if one or both of its operands are true. In this example, the first operand right here is the relational operation x greater than or equal to y. And the second operand is the relational operation, x greater than or equal to z. 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. 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. Well, let's try our functions. We'll do the last one first. Well, that makes sense. Three is not smaller than two and one. And that makes sense because minus one is smaller than two and one. And finally, that looks good because two is not smaller than two and one. 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. 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. 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. Let's clear the screen and look at some examples of logical operators used in this way. [NOISE]. We'll just type some expressions directly in to the command window. No surprises here. When both of the operands, the and operator, are non-zero, which is equivalent to true, the operator gives true in the form of the number one. But if either or both of the operands are zero, which is equivalent to false, then the operator gives false in the form of the number zero. Now let's give the same operands to the or operator. Here again, we get what we would expect. 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. Now, let's build a more complicated expression and get the logical NOD operator involved too. [NOISE] Let's see why D is zero. Since both A and B are non zero, they're both true. So, A and B is true too. 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. So it gives fault in the form of the number zero. That value of zero, is then assigned a d. 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. All really as we would expect. In fact, logical operators tend to act well, logically. They do what you'd expect them to do. Because we're so familiar with the concepts band and or. 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. So why would you want to mix arithmetic and logic? Well, suppose you wanted to add up numbers that were between zero and ten? 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. [NOISE] But 11 fails the test, so 0 is added. So as you can see, b is added only when it's inside the limits, just what we wanted to do. You can do the same thing with an if statement, but it's simpler to just do it in this one line. 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. Everything else goes first, as we'll see when we revisit the precedence table in a few minutes. There's one last feature of logical operators that we need to consider, they're used with arrays. We can see how it works with vectors, so we'll start off with those. We'll start the the not operator. Let's get a clean slate. [SOUND] There. Okay here's a nice vector. [SOUND] Now let's apply the not operator to it. It changes true to false and vice versa. And that's what's happened here. The nonzero numbers 1 pi and minus 2 represent true. So they're changed to faults in the form of a zero. And the zero here is changed to a one. Since not operates on individual elements in an array, it's an array operator. Now let's try the and operator with arrays. [NOISE] Hm. Operands to the or and, and operators must be convertible to logical scaler values. What does that mean? Well it means that the operands of these operators have to be scalers. They can't be arrays. Well, it's time to reveal the MATLAB actually has two versions of the logical operators. The versions you've not seen yet are array operators. Here's what the array versions of and, and or look like. First, let's get the original version and then we'll alter it, then we'll alter that one. And there we have it. As you can see, there's a single ampersand for and, and a single vertical bar for or. 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. These single symbol versions of the operators work just fine with arrays, as long as there are two operands have the same dimensions, which is the same rule as for all array operators. And they work with scaler array combinations too. [SOUND]. Here we can see that in the first expression, two was ANDed with each one the elements of the array. And in the second expression, two was ORed with each element of the array. 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, 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. Now the true value from this expression over here, 1.4 less than square root of 2 will be ANDed with the true and with the false elements of the vector, to give true and false. Which in MATLAB land are one and zero. Okay time to hit return. And we get a vector with one and zero as its elements. The order in which the operators less than and ampersand are applied has an important effect on the result. Let's check that by inserting extra parentheses around the less than operator and its operands to force the less than operation to go before the and operation. This operation before this one. Well, that's the same result we got without the extra parentheses, because this order agrees with MATLAB's precedence rules, as we'll see in a minute. Now, let's force the ampersand to go before the less than. Here, we put parentheses around everything over here after the less than right here, so that everything inside these parentheses is done first, and then, fed in as the right hand operand of the less than operation. And this is what we get. 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. Now that we've seen relational and logical operators, it's time to revisit operator precedence, which you may remember is the set of rules that governs the order in which operations are executed. This precedence table shows that order. Lower numbers mean earlier execution. For example, relational operators at six are executed before logical operators at seven, eight, nine, and ten. An addition at four is executed before the colon operator at five. Remember that surprising example a few lessons ago? Chances are, you're not going to learn this table by heart. But you can get it any time you want it by giving MATLAB the command Help Precedence. 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]
Info
Channel: Fitzle LLC
Views: 43,040
Rating: 4.891892 out of 5
Keywords: MATLAB (Programming Language)
Id: Rt2kUq5Hyr4
Channel Id: undefined
Length: 34min 52sec (2092 seconds)
Published: Sat Jun 13 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.