Department of Chemical Engineering
MATLAB Tutorial
 

Relational and Logical Operations                                                         (last updated   9/9/99)

The information in this tutorial is located in the MATLAB manual. Any page or section numbers refer to the following:

The Student Edition of MATLAB, Version 5, The MATH WORKS Inc. Prentice-Hall, 1997.

======================================================================================

This tutorial contains the following sections;

Introduction

Relational Operators

Logical Operators

======================================================================================

Introduction

Like other programming languages MATLAB supports relational and logical operations. These can be used to provide answers to True/False questions. This feature can be used to control the order of execution of MATLAB commands. As input, any non-zero number is taken to be True, and zero to be False. The output from all relational and logical operations is 1 for True and 0 for False.

======================================================================================

Relational Operators - Section 8.1

MATLAB uses the following relational operators.

<        less than
<=     less than or equal
>       greater than
>=    greater than or equal
==    equal to         (Note: This is a double equal sign)
~-=  not equal to

2 arrays can be compared. This is done on an element by element basis. A scalar can be compared to an array. The scalar is compared to each element.

>> a = 1:9
a =
   1   2   3   4   5   6   7   8   9

>> tf = a > 4
tf =
   0   0   0   0   1   1   1   1   1
 

You can also perform calculations with the result.

>> b = (8:-1:0)
b =
   8   7   6   5   4   3   2   1   0

>> tf = b - (a>2)
tf =
    8   7    5    4   3   2   1   0   -1
 

The first two elements of tf are (b - 0) and the last 7 elements are (b - 1).

One use would be to set a segment of an array to zero. For example, all negative values could be set to zero, while all positive values are left alone.

>> t = [5.68   3.54   1.03   0.24   -0.35   -0.80   -1.46   -2.57];
>> t = t .* (t > 0)
t =
   5.6800   3.5400   1.0300   0.2400   0    0    0    0
 

Another use is to replace zero elements by eps to avoid divide by zero errors.

>> x = (-3:3)/3
x =
   -1.0000    -0.6667    -0.3333    0    0.3333    0.6667    1.0000

>> sin(x) ./ x
ans =
   0.8415   0.9276   0.9816   NaN   0.9816   0.9276   0.8415
 

The fourth element (0/0) should have a value of 1.0000 (remember L'Hopital's rule). By using the relational operation (==) we can set the fourth element to eps.

>> x = x+(x==0)*eps
x =
   -1.0000   -0.6667   -0.3333   0.0000   0.3333   0.6667   1.0000
 

This looks the same as the response to line 27, but the fourth element has been changed from zero to eps. L'Hopital's rule is correctly applied.

>> sin(x) ./ x
ans =
   0.8415   0.9276   0.9816   1.0000   0.9816   0.9276   0.8415

======================================================================================

Logical Operators - Section 8.2

Logical operators provide a method to negate or combine relational operators.

The MATLAB logical operators are:

&    AND
|      OR
~    NOT
 

Example: To negate a relational operation

>> a = [-2:4], b = [0:6]
a =
   -2   -1   0   1   2   3   4
b =
   0   1   2   3   4   5   6

>> c = (a >= 1)
c =
   0   0   0   1   1   1   1

>> c = ~(a >= 1)
c =
   1   1   1   0   0   0   0

The ~ symbol basically reverses the True/False statement.
 

Example: To combine relational operations

>> c = (a >= 1) & (b >= 1)
c =
   0   0   0   1   1   1   1

>> c = (a >= 1) | (b >= 1)
c =
   0   1   1   1   1   1   1
 

The table on page 83 of the MATLAB manual lists a number of additional relational and logical operators that are contained in MATLAB. These include:

Returns 1 where either x or y is nonzero. Returns 0 where they are both either nonzero or both zero. Returns 1 if any element of the vector x is nonzero. For a matrix it works on a column by column basis. Returns 1 if all elements in a vector x are nonzero. For a matrix it works on a column by column basis.