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 operator | Description | Example | 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.
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.
The arguments to relational operators must either be scalars or arrays of the same size, or an array and a scalar.
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 .
Logical operators
Logical operators are used to combine the results of several comparisons.
Logical operator | Description | Example |
& | and | 1 & 0 |
| | or | 1 | 0 |
~ | not | ~1 |
The operator ~ gives the logical negation (not). Thus ~1 gives 0 and ~0 gives 1.
The logical and & is true only if both arguments are true.
The logical or | is true if either or both arguments are true.
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.
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
- MATLAB actually treats any non-zero value as true, although it always uses 1 to represent a logical true value.
- Using == to compare floating point (real) quantities can produce unexpected
results because of the effect of rounding errors.
Is 0.6/0.1 equal to 6 on a computer?
0.1 and 0.6 are not stored exactly in floating point arithmetic on a computer, so 0.6/0.1 is not exactly 6.
>> (0.6/0.1) == 6
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.