Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

matlab
communitycreator
logicaloperator

What are logical operators in MATLAB?

Zain Ali Babar

Overview

Operators are special keywords or characters that perform specific functions. MATLAB supports the use of operators on both scalar and non-scalar data. The logical operators supported in MATLAB are listed and described below:

Logical operators

Symbol

Description

&

Logical AND

|

Logical OR

~

Logical NOT

&&

Logical AND with short-circuiting

||

Logical OR with short-circuiting

Logical AND (&)

The operator & is used to determine the logical “AND” of arrays or numbers.

In the case of a single number, the AND operation is set to true or 1 if both elements on the left and right sides of the & operator are non-zero. Otherwise, it returns 0.

In the case of an array, the corresponding array entries are compared, and the AND operation is set to true or 1 if both elements on the left and right sides of the & operator are non-zero. Otherwise, it returns 0.

// AND of scalar and scalar
 X = 5
 Y = 4
 X & Y 
// Answer will be 1
 X = 5
 Y = 0
 X & Y 
// Answer will be 0

// AND of scalar and array
 X = [5 0 4]
 Y = 5
 X & Y
// Answer will be [1 0 1]

//AND of array and array
 X = [5 1 0]
 Y = [0 7 9]
 X & Y
// Answer will be [0 1 0]

Logical OR (|)

The operator | is used to determine the logical “OR” of arrays or numbers.

In the case of a single number, the OR operation is set to true or 1 if at least one element on the left or right side of the | operator is non-zero. Otherwise, it returns 0.

In the case of an array, the corresponding array entries are compared, and the OR operation is set to true or 1 if at least one element on the left or right side of the | operator is non-zero. Otherwise, it returns 0.

// OR of scalar and scalar
 X = 5
 Y = 0
 X | Y 
// Answer will be 1
 X = 5
 Y = 5
 X | Y 
// Answer will be 1

// OR of scalar and array
 X = [5 0 4]
 Y = 0
 X | Y
// Answer will be [1 0 1]

//OR of array and array
 X = [5 1 0]
 Y = [0 7 9]
 X | Y
// Answer will be [1 1 1]

Logical NOT (~)

The operator ~ determines the logical “NOT” of arrays or numbers.

In the case of a single number, the NOT operator returns 0 if the value is non-zero and returns 1 if the value is zero.

In the case of an array, an array is returned, in which the index values are 0 where values in the original array were non-zero. The index values of the returned array are 1 where values were zero in the original array. We can see this in the example below.

// NOT of scalar 
 X = 5
 ~X 
// Answer will be 0
 X = 0
 ~X 
// Answer will be 1

//NOT of array
 Y = [0 7 9]
 ~Y
// Answer will be [1 0 0]

Logical AND with short-circuiting (&&)

The operator && is used to determine the logical “AND” of arrays or numbers. MATLAB uses the short-circuiting method, i.e., in case of an expression A && B, if any value from A is 0, then B is not used, as the AND is guaranteed to be 0. This is further explained below.

In the case of a single number, the && operation is set to true or 1 if both elements on the left and right sides of the && operator are non-zero. Otherwise, it returns 0.

In the case of an array, the corresponding array entries are compared, and the && operation returns true or 1 if all elements of the arrays being compared are non-zero. Otherwise, it returns 0.

The following example demonstrates this.

As can be seen below, as soon as one zero element is found, the operation is completed, and the remaining elements of the array do not matter, causing the function to return 0. This is known as short-circuiting.

// AND (&&) of scalar and scalar
 X = 5
 Y = 0
 X && Y 
// Answer will be 0
 X = 5
 Y = 5
 X && Y 
// Answer will be 1
// Note that for scalar && is similar to &

// AND (&&) of scalar and array
 X = [5 0 4]
 Y = 1
 X && Y
// Answer will be 0
 X = [5 4 4]
 Y = 1
 X && Y
// Answer will be 1

//OR of array and array
 X = [5 1 0]
 Y = [0 7 9]
 X && Y
// Answer will be 0
 X = [5 1 4]
 Y = [4 7 9]
 X && Y
// Answer will be 1

Logical OR with short-circuiting (||)

The operator || is used to determine the logical “OR” of arrays or numbers. MATLAB uses the short-circuiting method, i.e., in the case of the expression A || B, if all values from A are 1, then B is not used as the OR is guaranteed to be 1.

In the case of a single number, the || operation is set to true or 1 if at least one element on either the left or the right side of the || operator is non-zero. Otherwise, it returns 0.

In the case of an array, the corresponding array entries are compared, and the || operation returns true or 1 if at least one of the arrays being compared has no non-zero values. Otherwise, it returns 0. This is further explained in the example below.

As soon as one non-zero array is found, the operation is completed, and the second array being compared does not matter, causing the function to return 1. This is known as short-circuiting.

Note: The || operator will return 0 when both numbers or arrays being compared are 0.

// OR (||) of scalar and scalar
 X = 5
 Y = 0
 X || Y 
// Answer will be 1
 X = 0
 Y = 0
 X || Y 
// Answer will be 0
// Note: for scalar || is similar to |

// OR (||) of scalar and array
 X = [5 0 4]
 Y = 1
 X || Y
// Answer will be 1
 X = [0 0 0]
 Y = 0
 X || Y
// Answer will be 0

//OR of array and array
 X = [5 1 1]
 Y = [0 0 0]
 X || Y
// Answer will be 1
 X = [5 1 1]
 Y = [0 1 0]
 X || Y
// Answer will be 1
 X = [5 1 0]
 Y = [0 7 9]
 X || Y
// Answer will be 0
 X = [0 0 0]
 Y = [0 0 0]
 X || Y
// Answer will be 0

RELATED TAGS

matlab
communitycreator
logicaloperator
RELATED COURSES

View all Courses

Keep Exploring