Logical Operations
This section covers basic logical operators available in MATLAB as well as logical indexing and some basic logical functions.
Basic logical operations
Logical operators in MATLAB are used to make comparisons between objects such as scalars, vectors or matrices. Logical operators are binary operators i.e. they return a boolean value which is either true (which has numeric value 1) or false (which has numeric value 0). When logical operators are applied to vectors, matrices or arrays, the operations are carried out element-by-element, therefore the matrices/vectors must have the same dimensions.
The basic logical operators are the following.
Math
MATLAB
Description
>
Greater than
>=
Greater than or equal to
<
Less than
<=
Less than or equal to
==
Equal to
~=
Not equal to
Consider the following examples.
Combining logical expressions
Logical expressions can be combined to evaluate more complex conditions by chaining them using one of the following three operators.
Math
Array operator
Scalar operator
AND
&
&&
OR
|
||
NOT
~
~
Even though the array operators can also be used when comparing two scalars, it is recommended to use the scalar operators in this case as they are computationally efficient. To understand better how these operators work, lets evaluate the following logic tables, by combining the elements of the following logical matrices which give all possible comparisons between two logical values.
Consider the following examples for chaining logical expressions.
Logical Indexing
We can use logical expressions to access elements of a vector or matrix. Instead of providing the numeric indices of which element to select, we provide a boolean true/false value for each element of the matrix, choosing whether it should be selected (true i.e. 1) or not (false i.e. 0). Consider the following examples.
Note here the use of the logical
function in the second example which turns any numeric 1 into a boolean true (boolean 1) and any numeric 0 into a boolean false (boolean 0).
Logical Functions
MATLAB provides some functions which are very useful when working with logical expressions. Here is a summary of the most important functions.
all and any
all(X)
and any(X)
take a logical vector or matrix as an input and return column-by-column whether all elements of that column are true (in the case of all) and whether any element of that column is true. This can be particularly useful for checking whether a condition is met in the column of a matrix or the whole vector/matrix. It can also sometimes be useful to nests these commands to get more complex conditions.
is-functions
MATLAB provides a collection of functions that can be used to check whether a vector/matrix has special characteristics. The details for each function should be checked by consulting the documentation or using the help command.
The following commands summarize properties of the whole matrix and hence return a scalar boolean value whether the condition is met or not.
isreal(X)
- Logical 1 if array is real-valued, 0 otherwiseischar(X)
- Logical 1 if array is character array, 0 otherwiseisempty(X)
- Logical 1 if array is empty, 0 otherwiseisequal(X,Y)
- Logical 1 if all elements in array are equal, 0 otherwiseislogical(X)
- Logical 1 if array is a logical array, 0 otherwiseisscalar(X)
- Logical 1 if argument is scalar, 0 otherwiseisvector(X)
- Logical 1 if argument is row- or column-vector, 0 otherwiseisrow(x)
- Logical 1 if argument is a row-vector, 0 otherwiseiscolumn(x)
- Logical 1 if argument is a column-vector, 0 otherwiseismatrix(X)
- Logical 1 if argument is matrix (or vector), 0 otherwise
Consider the following examples.
There are also logical functions which work element-by-element and hence output a matrix of the same dimensions as the input matrix.
isnan
- Logical 1 if element is NaN, 0 otherwiseisinf
- Logical 1 if element is Inf, 0 otherwiseisfinite
- Logical 1 if element is not Inf, 0 otherwise
Consider the following examples
find
This is a very useful function that takes logical inputs and returns the matrix indices where the logical statement is true. There are two ways to call the function.
ix = find(B)
- saves indices of elements (column-wise) for which B is true[ir, ic] = find(B)
- saves row and column indices of the elements for which B is true
Last updated
Was this helpful?