English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MATLAB Logical Operations

Matlab Operators

MATLAB provides two types of logical operators and functions-

  • Element-wise-These operators operate on the corresponding elements of logical arrays.

  • Short-circuit-These operators operate on scalar logical expressions.

Element-wise logical operators operate element-wise on logical arrays. The symbols &,| and ~ are logical array operators AND, OR and NOT.

Short-circuit logical operators allow short-circuiting of logical operations. The symbols && and || are logical short-circuit operators AND and OR.

Online Example

Create a script file and enter the following code-

a = ; 5;
b = ; 20;
   if ((a && b))
      disp('Line 1 - Condition is true');
   end
   if ((a || b))
      disp('Line 2 - Condition is true');
   end
   
   % lets change the value of  a and b 
   a = 0;
   b = ; 10;
   
   if ((a && b))
      disp('Line 3 - Condition is true');
   else
      disp('Line 3 - Condition is not true');
   end
   
   if (~((a && b)))
   
      disp('Line 4 - Condition is true');
   end
When you run the file, it will produce the following results-
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true

Logical Operation Functions

In addition to the above logical operators, MATLAB also provides the following commands or functions for the same purpose-

Serial NumberFunction Description
1

and(A, B)

Find the logical AND of array or scalar input; perform logical AND on all input arrays A, B, etc., and return an array containing elements set to logical1(true) or logical 0(false) elements. If all input arrays contain non-zero elements at the same array position, the elements of the output array are set to1. Otherwise, the element is set to 0.

2

not(A)

Find the logical NOT of array or scalar input; perform logical NOT operation on the input array A and return an array containing elements set to logical1(true) or logical 0(false) elements. If the input array contains a zero value element at the same array position, the element of the output array is set to1. Otherwise, the element is set to 0.

3

or(A, B)

Find the logical OR of array or scalar input; perform logical OR on all input arrays A, B, etc., and return an array containing elements set to logical1An array of elements that are either (true) or logical 0 (false). If any input array contains a non-zero element at the same array position, the element of the output array is set to1. Otherwise, the element is set to 0.

4

xor(A, B)

Logical exclusive OR; performs an exclusive OR operation on the corresponding elements of arrays A and B. If A(i,j,...) or B(i, j, ...) is non-zero, but not both.

5

all(A)

Determine if all array elements of array A are non-zero or true.

  • If A is a vector, all(A) then if all elements are not zero, then return logical1(true), if one or more elements are zero, then return logical 0 (false).

  • If A is a non-empty matrix, then consider the columns of Aall(A) as vectors and return logical1And the row vector of 0.

  • If A is an empty 0×0 matrix, then all(A) returns logical1(true).

  • If A is a multi-dimensional array, then all(A) performs the operation along the first non-single dimension and returns an array of logical values. The size of this dimension is reduced to1, while the sizes of all other dimensions remain unchanged.

6

all(A, dim)

Along the scalardimTest the specified A dimensions.

7

any(A)

Determine if any array elements are non-zero; otherwise, it is 0. Test if any elements in the array A are non-zero numbers or logical1(true). The any function ignores NaN (non-numeric) entries.

  • If A is a vector, then if any element of Aany(A) is a non-zero number or a logical1(true), then return logical1(true), if all elements are zero, then return logical 0 (false).

  • If A is a non-empty matrix, then consider the columns of Aany(A) as vectors and return logical1And the row vector of 0.

  • If A is an empty 0×0 matrix, then any(A) returns logical 0 (false).

  • If A is a multi-dimensional array, then any(A) performs the operation along the first non-single dimension and returns an array of logical values. The size of this dimension is reduced to1, while the sizes of all other dimensions remain unchanged.

8

any(A,dim)

Along the scalardimTest the specified A dimensions.

9

false

Logical 0(false)

10

false(n)

Is a logical zero n×n matrix

11

false(m, n)

Is a logical zero m×n matrix.

12

false(m, n, p, ...)

Is a logical zero m×n×p×... array.

13

false(size(A))

Is a logical zero array with the same size as array A.

14

false(...,'like',p)

Is a logical zero array with the same data type and sparsity as logical array p.

15

ind  = find(X)

Find the indices and values of non-zero elements; find all non-zero elements of the array X and return the linear indices of these elements in the vector. If X is a row vector, the returned vector is a row vector; otherwise, return X. Otherwise, return a column vector. If X does not contain any non-zero elements or is an empty array, return an empty array.

16

ind = find(X, k)

ind = find(X, k, 'first')

Returns the first k indices corresponding to the non-zero entries of X. k must be a positive integer, but can be any numeric data type.

17

ind = find(X, k, 'last')

Returns at most the last k indices corresponding to the non-zero entries of X.

18

[row,col] = find(X, ...)

Returns the row and column indices of non-zero entries in the matrix X. This syntax is especially useful when using sparse matrices. If X is N> 2If the N-dimensional array, then col contains the linear index of the columns.

19

[row,col,v] = find(X, ...)

Returns the column or row vector v of non-zero entries in X and the row and column indices. If X is a logical expression, then v is a logical array. The output v contains the non-zero elements of the logical array obtained by evaluating the expression X.

20

islogical(A)

Determines whether the input is a logical array; if A is a logical array, it returns true, otherwise it returns false. If A is an instance of a class derived from the logical class, it also returns true.

21

logical(A)

Converts a numeric value to a logical value; returns an array that can be used for logical indexing or logical testing.

22

true

Logical1(true)

23

true(n)

Is an n×n matrix of logical matrices.

24

true(m, n)

Is an n×n matrix of logical matrices.

25

true(m, n, p, ...)

Is an array of m×n×p×... logical arrays.

26

true(size(A))

Is a logical array with the same size as array A.

27

true(...,'like', p)

Is a logical array with the same data type and sparsity as logical array p.

Matlab Operators