# Relational Operators and Logical Variables Matlab Help

Relational Operators and Logical Variables

MATLAB has six relational operators to make comparisons between arrays. These operators are shown in Table 4.2-1 and were introduced in Section 1.3. Recall that the equal to operator consists of two = signs, not a single = sign as you might expect. The single =sign is the assignment, or replacement, operator in MATLAB.

Relational operators

The result of a comparison using the relational operators is either 0 (if the comparison is false), or 1 (if the comparison is true), and the result can be used as it variable. For example, if x = 2 and y = 5, typing z = x < y returns the value z = 1 and typing u = x==y returns the value u = O.To make the statements more readable, we can group the logical operations using parentheses.

For example, z = (x < y) and u = (x==y).
When used to compare arrays, the relational operators compare the arrays on an element-by-element basis. The arrays being compared must have the same dimension. The only exception occurs when we compare an array to a scalar. In that case all the elements of the array are compared to the scalar. For example,suppose that x = [6,3,9] and y = [14,2,9]. The following MATLAB session shows some examples.

»z = (x < y)
z =
1  0 0
»z = (..X -= y)
z =
1  1  0
»z = (x > 8)
z =
0  0  1

The relational operators can be used for array addressing. For example, with
x = [6,3,9] and y = [14,2,9],typing z = x t x-c y ) finds all the elements
in x that are less than the corresponding elements in y. The result is z = 6.
The arithmetic operators +, -, *, /, and \ have precedence over the relational
operators. Thus the statement z = 5 > 2 + 7 is equivalent to z = 5· > (2 +7 )
and returns the result z = O. We can use parentheses to change the order of
precedence; for example, z = (5 > 2) + 7 evaluates to z =. 8.
The relational operators have equal precedence among themselves, and
MATLAB evaluates them in order from left to right. Thus the statement

z = 5 > 3 -= 1
. is equivalent to
z = (5)3) -= 1

Both statements return the result z = o.

With relational operators that consist of more than one character, such as =or >=, be careful not to put a space between the characters.

The logical Class

When the relational operators are used, such as x = (5 > 2), they create a logical variable, in this case, x. Prior to MATLAB 6.5 logical was a When the relational operators are used, such as x = (5 > 2), they create a logical variable, in this case, x. Prior to MATLAB 6.5 logical was an attribute of any numeric data type. Now logical is a first-class data type and a MATLAB class, and so logical is now equivalent to other first-class types such as,character and cell arrays. Logical variables may have only the values 1 (true) and 0 (false).
Just because an array contains only 0s and Is, however, it is not necessarily a logical array. For example, in the following session k and w appear the same, but k is a logical array and w is a numeric array, and thus an error message is issued.

»x = [-2:2]
x =
-2 -1 0 1 2
»k = (abs (x»l)
k =
1 0 0 0 1
»z = x(k)
z =
-2 2
»w [1: 0 , 0 , 0 , 1] ;
»v = x(w)
??? Subscript indices must either be real positive  integers or logicals.

The logical Function

The logical array can be created with the relational and logical operators and with the logical function. The logical function returns an array that can be used for logical indexing and logical tests used for logical indexing and logical tests. Typing B = logical (A) , where A is a numeric array, returns the logical array B. SO to correct the error in the previous session, you may type instead w = logical ( [1, 0, a 10 11] ) before typing v =’ x(w). . .

When a finite, real value other than I or 0 is assigned to a logical variable, the value is converted to logicaI and a warning message is issued. For example, when you type y = log i c a 1 (9 ) ,y will be assigned the value logical and a warning will be issued. You may use the double function to convert a logical array to an array of class double.Nor example, x = (5)3); y = double (x) ;.

Some arithmetic operations convert a logical array to a double array. For example, if we add zero to each element of B by typing B = B + 0, B will be converted to a numeric (double) array. However, not all mathematical operations are defined for logical variables. For example, typing

»x = ([2, 3] > [1, 6]);
»y = sin (x)
> will generate an error message

Accessing Arrays Using Logical Arrays

When a.logical array is used to address another array, it extracts from that array the elements in the locations where the logical array has 1s. So typing A(B) , where B is a logical array of the same size as A, returns the values of A at the indices where B is 1.
Given A = [5, 6,7; 8,9,10; 11,12,13] and B = logical(eye (3 )),
we can extract the diagonal elements of A by typing c = A (B) to obtain C = [5 ; 9; 13] . Specifying array subscripts with logical arrays extracts the elements that correspond to the true (I) elements in the logical array.

Note, however, that using the numeric array eye (3) , as C = A (eye (3) ) , results in an error message because the elements of eye (3) do not correspond to locations in A. If the numeric array values correspond to valid locations, you may use a numeric array to extract the elements. For example, to extract the diagonal elements of A with a numeric array, type C = A( [1, 5 , 9] ) .
MATLAB data types are preserved when indexed assignment is used. So now that logical is a MATLAB data type, if A” is a logical array, for example A = logical (eye (4) ) , then typing A (3 , 4) = 1 does not change A to a double
array. However, typing A (3 , 4 ) 5 will set A(3 , 4) to logical 1 and cause a warning to be issued.

Posted on July 27, 2015 in Programming with MATLAB