MATLAB Lesson 8 - Logic and Control

Logical true and false

MATLAB uses 0 to represent a logical false, for example 3 < 2, and 1 to represent a logical true, for example 2 < 3. MATLAB has operators for all the standard comparisons and for combining logical expressions. In MATLAB logical operators are applied to arrays (vectors or matrices) element by element.

 

Relational operators

The standard MATLAB relational operators are listed in the table below. More information can be found by typing help relop in the MATLAB command window or using the MATLAB help browser (search for Relational Operators).

Relational operatorDescriptionExample Result
< less than 2 < 3 1
<= less than or equal to 2 <= 3 1
> greater than 2 > 3 0
>= greater than or equal to 2 >= 3 0
== equal to 2 == 3 0
~= not equal to 2 ~= 3 1

If the arguments are arrays of the same size, then the operators are applied element by element, producing a logical array of the same size as the arguments with elements 0 or 1.

What does x < y produce for x = [1 2 3] and y = [0 4 3]?

x and y are 1 by 3 arrays (row vectors), so the comparisons are done element by element. The result is a logical (0/1) array of size 1 by 3.

The first element is 1 < 0 which is false giving a 0, the second element is 2 < 4 which is true giving 1, while the third element is 3 < 3 which is false giving 0.

>>  x = [1 2 3]
>>  y = [0 4 3]
>>  x < y


The arguments to relational operators must either be scalars or arrays of the same size, or an array and a scalar.

What does the following MATLAB code produce?

As u is a row vector, the comparisons are done element by element, producing a row vector of the same size as u, namely a 1 by 5 array.

The result is 0 0 1 1 1 .

>>  u = [1:5]
>>  u >= 3

 

Logical operators

Logical operators are used to combine the results of several comparisons.

Logical operatorDescriptionExample
& and 1 & 0
| or 1 | 0
~ not ~1

The operator ~ gives the logical negation (not). Thus ~1 gives 0 and ~0 gives 1.

Determine if the value of a variable x is between 0 and 2, that is x>0 and x<2.

The logical and & is true only if both arguments are true.

>>  (x>0) & (x < 2)

Determine if the value of a variable x is outside the interval [0, 2], that is x<0 or x>2.

The logical or | is true if either or both arguments are true.

>>  (x<0) | (x > 2)

What do the following MATLAB commands produce?

x and y are 1 by 4 arrays, so the results are logical (0/1) arrays of size 1 by 4.

The logical and & is true only if both arguments are true.

The logical or | is true if either one or both of the arguments are true.

>>  x = [1 1 0 0]
>>  y = [0 1 0 1]
>>  x & y
>>  x | y

There is a MATLAB function xor for the exclusive or, which is true if either one but not both of the arguments is true.

 

Warnings

 

Self-test Exercise

Write a MATLAB expression that is true if an element of the array X is in (-2, 4] and false otherwise. Be careful which endpoints of the interval are included.

Answer: (X>-2) & (X<=4)
Use the mouse to select the text between the word "Answer" and here to see the answer.

Summary

MATLAB uses 1 to represent a logical true and 0 to represent a logical false.

MATLAB includes all the standard relational operators, as well as & for logical and together with | for logical or.

Relational operations on arrays work element by element.