Logical Operators and Functions
MATLAB has five logical operators, which are sometimes called Boolean operators (see Table 4.3-1). These operators perform element-by-element operations. With the exception of the NOT operator (-), they have a lower precedence than the arithmetic and relational operators (see Table 4.3-2). The NOT symbol is called the tilde. The NOT operation -A returns an array of the same dimension as A; the new array has ones where A is zero and zeros where A is nonzero. If A is logical, then -A replaces ones with zeros and zeros with ones. For example
if X = [0,3,9] and y = [14,-2,9], then z = -x returns the array z = [1,0,0] and the statement u == -x > y returns the result u = [0,1,0]. This expression is equivalent to u = ( – x) > y, whereas v = – (x > y) gives the result v = [1, °,1] .This expression is equivalent to v = (x <= y). The & and I operators compare two arrays of the same dimension. The only exception, as with the relational operators, is that an array can be compared to a scalar. The AND operation A&B returns ones where both A and B have nonzero elements and zeros where any element of A or B is zero. The expression z = 0&3 returns z = 0; z = 2&3 returns z = 1; z = 0&0 returns z = O,and z = [5,-3,0,0]&[2,4,0·,5] returns z = [l,l,O,O].Because of operator precedence, z =’ 1& 2 +3 is equivalent to z = 1& ( 2 +3 ) • which returns z = 1. Similarly, z = 5<6&1 is equivalent to z (5<6) &1,\
which returns z = 1.
Let x = [ 6 , 3 , 9] and y = [ 14 , 2 , 9] and let a = [ 4 , 3 , 12 ]. The expression
z = (x>y) & a
gives z = [0′, 1, 0], and
z = (x>y}&(x>a)
returns the result z [‘0 , °,°J. This is equivalent to
z = x>y&x>a
which is much less readable.
Be careful when using the logical operators with inequalities. For example, note that – (x .> y) is equivalent to x <= y. It is not equivalent to x < y. As another example, the relation 5 < x < 10 must be written as
(5 < x) & (x < 10)
The OR operation A&B returns ones where at least one of A and B has nonzero elements and zeros where both A and Bare zero. The expression z = 013 returns z = 1; the expression z = °10 returns z = 0; and
z = [5,-3,0,0] I [2,4,0,5]
returns z = [ 1 , 1 , °,1] . Because of operator precedence,
z = 3<514==7
is equivalent to
which returns z = 1. Similarly, Z = 110&11s equivalent to Z =~ (110&1,
which returns z = 1, while z = 110&0 returns z = 0, and z = 0&011 returns z = 1.
Because of the precedence of the NOT operator, the statement
z = -3==714==6
.returns the result z = 0, which is equivalent to
The exclusive OR function xor (A, B) returns zeros where A and B are either both nonzero or both zero, and ones where either A or B is nonzero, but not both.The function is defined in terms of the AND, OR, and NOT operators as follows.
function z = xor(A,B)
z = (A&B) & -(A&B);
z = xor ( [3,0,6], [5, 0, 0] )
returns z = [0, 0, 1], whereas
z = [3,0,6]1 [5,0,0]
returns z = [ 1 , a r 1 ]
Table 4.3-3 is a so-called truth table that defines the operations of the logical operators and the function xor. Until you acquire more experience with the logical operators, you should use this table to check your statements. remember that true is equivalent to logical I, and false is equivalent to logical O. We can test the truth table .by building its numerical equivalent as follows. Let x and y represent the first two columns of the truth table in terms of ones and zeros.
The following MATLAB session generates the truth table in terms of ones and zeros.
»x = [1,1,0,0]’;
»y = [1,0,1, 0] ,;
»Truth_Table = [x,y,-x,xly,x&y,xor(x,y)]
100 1 ° 1
011 1 ° 1
001 ° ° °
Starting with MATLAB 6, the AND operator (s) was given a higher precedence than the OR operator. ( I). This was not true in earlier versions of MATLAB, so if you are using code created in an earlier version, you should make the necessary changes before using it in MATLAB 6 or higher. For example, now the statement y = 1 I 5& 0 is evaluated as y = 1 I (5 & 0 ) , yielding the result y = 1, whereas in MA1LAB 5.3 and earlier, the statement would have been evaluated as y = (115) &0, yielding the result y = O. To avoid potential problems due to precedence, it is important to use parentheses in statements containing arithmetic, relational, or logical operators, even where parentheses are optional. MATLAB now provides a feature to enable the system to produce either an error message or a warning for any expression containing & and I that would be evaluated differently than in earlier versions. If you do not use this feature, MATLAB will issue a warning as the default. To activate the error feature, type feature (‘OrAndError’ ,1) .To reinstate the default, type feature(‘OrAndError’ ,O).
The following operators perform AND and OR operations on logical expressions -containing scalar values only. They are called short-circuit operators because they evaluate their second operand only 1 then the result is not fully determined by the first operand. They are defined as follows in terms of the two logical variables A
A&&B Returns true logical if both ,”\and B evaluate to true, and false (logical 0) if they do not.
A I IB Returns true (logical I) l’ either A or B, or both, evaluate to true, and false (logical 0) if they do not.
Thus in the statement A&&B, if A equals logical zero, then the entire expression will evaluate to false. regardless of the value of B, and therefore there IS no need to evaluate B.
For AI I B. if A is true, regardless of the value of B, the statement will evaluate to true.
Table 4.3-4lists several useful logical functions. You learned about the find function in Chapter 1
Logical Operators and the find Function
The find function is very useful for creating decision-making programs, especially when combined with the relational or logical operators. The function find x)”computes an array containing the indices of the nonzero elements of the array x. We saw examples of its use with relational operators in Chapter 1. It is also useful when combined with the logical operators. For example, consider the session
»x = [5, -3, 0, 0, 8); y = [2,4,0,5,7);
»z = find (x&y)
1 2 5
The resulting array z ,= [1, 2, 51 indicates that ‘the first. second. and fifth elements of x and y are both nonzero. Note that the find function returns the indices. and not the’values. In the following session. note the difference between the result obtained by y (X&y) and the result o,b~ned by find (x&y) above
»x = [5, -3, 0, 0, 8]; y = [2, 4, 0, 5, 7];
»values = y (x&y)
2 4 7
» how_many = length. (vaIues)
Thus there are three nonzero values in the, array y that correspond to nonzero values in the array x. They are the first, second, and fifth values, which are 2, 4,
In the above example, there were only a few numbers in the arrays x and y, and thus we could have obtained the answers by visual inspection, However. these MATLAB methods are very useful either where there is so much data that visual inspection ‘would be very time-consuming, or where the values are generated internally in a program.
Test Your Understanding
14.3-1 If x = [ 5, – 3, 18, 41 and y – [- 9, 13 , 7, 4]. what will be the
result of the following operations? Use MAn.AB to check your answer.
a. z =’ -y > x “/ ‘
b. z = x&y / .
C. Z = xly
d. z = xor(x,y)
Test Your Understanding
Consider the problem given in Example 4.3-1. Userelational and logical operators to find the times for which either the projectile’s height is less than 4 m or the speed is greater than 17 m/s. Plot h(t) and v(t) to confirm