CHAPTER 5

Logical Operators and Functions

5.1 The Logical Operators: not, and, or, exclusive or
5.2 Associated Logical Functions: any, all, find
5.3 Looking for Special Numbers: NaN and Inf



MATLAB supplies the usual logical operations that find use particularly in writing MATLAB programs. These consist of logical operators used for comparisons as shown next:

Logical Comparison Operations
	Example			Symbol		Meaning 

>> a = [1 2 3] ; Equality of numerics -- >> disp(2 == a) Thus, only the second 0 1 0 == element of vector a is equal to 2.
>> disp('b'=='abB') 0 1 0 == Equality of character data
>> disp(2 <= a) 0 1 1 <= Less than or equal for numerics
>> disp('b'<='abcB') 0 1 1 0 <= Less than equal for character data



The examples shown in the table are typical, but the other comparisons shown next:

Symbol

Meaning

<

Less than

>

Greater than

>=

Greater than or equal

~=

Not equal


may also be done.

The six logical operators all perform element by element comparisons of elements in the same positions in arrays. They also give us a direct comparison of a scalar with all the elements in any array.

The logical comparisons of scalars are what we would expect. They all give 0 for false or 1 for true:

>> disp(3<5) 
     1 
 
>> disp(5<3) 
     0 
 
>> disp(3<3) 
     0 
 
>> disp(3>=3) 
     1 
 
>> disp(3==3.1) 
     0 
 
>> disp(3~=3.1) 
     1 

All of those are easy to understand.

The logical operators all perform directly as we would expect on vectors as long as they are the same length.

>> disp([1 2]<[-2 2])
     0     0 
 
>> disp([1 2]<=[-2 2])
     0     1 
 
>> disp([1 2]>[-2 2])
     1     0 
 
>> disp([1 2]>=[-2 2])
     1     1 
 
>> disp([1 2]==[-2 2])
     0     1 
 
>> disp([1 2]~=[-2 2])
     1     0 

The logical operators all do quite nicely in comparing a scalar to the elements of a vector, but you need to tell whether the comparison or the vector generator is to be done first:

>> disp(2>0:4) 
 
??? disp(2>0:4)
Improper function reference. A "," or ")" is expected. 
 
>> disp(2>(0:4)) 
     1     1     0     0     0 

Additional parentheses are needed to made this clear.

Operations on each element of matrices is shown next:

>> m=[1 2 3 
      2 3 4]; 
 
>> disp(m<2) 
     1     0     0 
     0     0     0 
 
>> m1=[-1 0 2 
        0 2 4]; 
 
>> disp(m>m1) 
     1     1     1 
     1     1     0 



5.1 The Logical Operators: not, and, or, exclusive or

We may also want to string the logical results with 'not,' 'or,' 'and,' and `exclusive or' operations. The ways that `not,' 'or,' 'and,' and `exclusive or' operations perform are shown in the next Matlab session:

More Logical Operations

>> disp([0 0 1 1] & [0 1 0 1])     <-- & = Logical `and' 
     0   0   0   1 	
 
>> disp([0 0 1 1] | [0 1 0 1])     <-- | = Logical `or' 
     0   1   1   1 	
 
>> disp(~[0 1])                  <-- ~ = Logical `not' 
     1   0 	
 
>> disp(xor([0 0 1 1],[0 1 0 1]))   <-- xor = Logical `exclusive or' 
     0   1   1   0 

The `not' operator (~) helps transform answers that we get for logical comparisons to ones that we need. It can be applied to any array:

>> disp(m) 
     1     2     3 
     2     3     4 
 
>> disp(~0) 
     1  
 
>> disp(~[0 1]) 
     1     0 
 
>> disp(~m>2)  
     0     0     0  
     0     0     0  
 
>> disp(~(m>2)) 
     1     1     0  
     1     0     0 

The examples shown with the matrix m, illustrate two characteristics of MATLAB. In MATLAB logical operations, any non-zero number is treated as being 'true'. Secondly, the not operation is done before the comparison if there are no parentheses as in the first case with m. Thus from ~m, we got all zeros or ones and then none of those were found to be greater than 2.

The and operation on logical elements is done with the symbol: "&" . The or operation on logical elements is done with the symbol: "|" . These operations work with pairs of logical elements to produce results as seen in the vector operations used in our table:

>> disp([0 0 1 1]&[0 1 0 1]) 
     0     0     0     1 
 
>> disp([0 0 1 1]|[0 1 0 1]) 
     0     1     1     1 

These extend to pairs of matrices of the same shape and to a matrix combined with a scalar with no problems:

>> m=[0 0 
      1 1]; 
 
>> m1=[0 1 
       0 1]; 
 
>> disp(m&m1) 
     0     0 
     0     1 
 
>> disp(m|m1) 
     0     1 
     1     1 
 
>> disp(m&1) 
     0     0 
     1     1 

The exclusive or operation on logical elements is done with the function: xor. This operation accepts a pair of logical elements to produce results as seen in the vector operations used in our table:

>> disp(xor([0 0 1 1],[0 1 0 1]))
     0     1     1     0 

Be careful to note xor is a function, and the two vectors of logical elements must be entered in standard function format.


5.2 Associated Logical Functions: any, all, find

These three functions find frequent use in making programming decisions about what needs to be done in solving problems. The function any tells us whether there are any non-zero elements in a vector as in:

>> v=[-2 1 3 5]; 
 
>> disp(any(v<1)) 
     1 
 
>> disp(any(v>6)) 
     0 

Since the logical comparison returns 0 for each false comparison and 1 for each true comparison, the function any allows us to find if any of the comparisons are true. The all function tells us if every comparison is true:

>> disp(all(v<1)) 
     0 
 
>> disp(all(v<6)) 
     1 

Both of these functions can be applied to matrices. They simply tell us the condition found for each column of the matrix.

>> disp(m) 
     1     2     3 
     2     3     4 
 
>> disp(any(m<2)) 
     1     0     0 

The find function allows us to locate non-zero elements in an array. It gives the indices of all non-zero elements as seen next:

>> disp(v) 
    -2     1     3     5 
 
>> disp(find(v>3)) 
     4 
 
>> disp(find(v>0)) 
     2     3     4  

You cannot use find with matrices:

>> disp(m)  
     1     2     3 
     2     3     4 
 
>> disp(find(m>2)) 
 
??? Error using ==> find 
Argument must be a vector. 




5.3 Looking for Special Numbers: NaN and Inf


We have already run into two special numbers that can be generated by arithmetic operations. Dividing a finite number by zero can lead to an infinite "number" indicated by MATLAB as Inf . We get a warning message when this occurs. It would be nice to be able to find which element in an array took on such a value. Dividing 0 by 0 leads to another special case: NaN (otherwise know as Not a Number.) Both these special numbers can be seen in:

>> ex=[0 1 2]./[0 2 0] 
 
Warning: Divide by zero 
ex = 
       NaN    0.5000       Inf 

The functions isnan , finite and isinf allow us to look for such special cases:

>> disp(isnan(ex)) 
     1     0     0 
 
>> disp(finite(ex)) 
     0     1     0 
 
>> disp(isinf(ex)) 
     0     0     1 

Note also that the logical operations work in a reasonable fashion with the exceptional numbers.

>> disp(ex==NaN) 
     0     0     0 
 
>> disp(Inf>ex) 
     0     1     0 
 
>> disp(Inf>=ex) 
     0     1     1 
 
>> disp(NaN>ex) 
     0     0     0 
 
>> disp(NaN>=ex) 
     0     0     0 
 
>> disp(NaN==NaN) 
     0     

The NaN value essentially can not be compared with any other number, including itself . It is not equal to another copy of the same thing, and it is not greater than or equal to another NaN. The NaN value is unique in that it is the only value which is not equal to itself.


Continue on to Chapter 6
Return to Table of Contents