logo for matrixlab-examples.com
leftimage for matrixlab-examples.com

Boolean Operator

1.- Boolean operations in Matlab

2.- Video

3.- Relational operations

1.- Introduction to Boolean operations

In Matlab, there are four logical or boolean operators:

Boolean operator: Meaning:
      & logical AND
      |  logical OR
      ~ logical NOT (complement)
      xor exclusive OR

These operators produce vectors or matrices of the same size as the operands, with 1 when the condition is true, and 0 when the condition is false.

2.- Video

This video gives you an overview of the operations that can be accomplished with the software, and after that, I'll show more details and exact code...

Now, let's see more details and specific examples.

Given array x = [0 7 3 5] and array y = [2 8 7 0], these are some possible operations:

Operation:              Result:
n = x & y               n = [0     1     1     0]
m = ~(y | x)            m = [0     0     0     0]
p = xor(x, y)           p = [1     0     0     1]

Since the output of the logical or boolean operations is a vector or matrix with only 0 or 1 values, the output can be used as the index of a matrix to extract appropriate elements. For example, to see the elements of x that satisfy both the conditions (x<y) and (x<4), you can type x((x<y) & (x<4)).

Operation:              Result:
ans = [1     1     1     0]
x<4                     ans = [1     0     0     0]
q = x((x<y) & (x<4))    q = [0     3]

Additionally to these boolean operators, there are several useful built-in logical functions, such as:

any   true if any element of a vector is true
all    true if all elements of a vector are true
exist        true if the argument exists
isempty true for an empty matrix
isinf   true for all infinite elements of a matrix
isnan true for all elements of a matrix that ara not-a-number
find  finds indices of non-zero elements of a matrix

3.- Relational Operators

There are six relational operators in Matlab:

Relational operator: Meaning:
              <  less than
              <= less than or equal
              > greater than
              >= greater than or equal
              ==  equal (possibility, not assignation)
              ~= not equal

These operations result in a vector of matrix of the same size as the operands, with 1 when the relation is true, and 0 when it’s false.

Given arrays x = [0 7 3 5] and y = [2 8 7 0], these are some possible relational operations: 

Operation:              Result:
k = x<y                
k = [1     1     1     0]
k = x <= y              k = [1     1     1     0]
k = x == y              k = [0     0     0     0] 

Although these operations are usually used in conditional statements such as if-else to branch out to different cases, they can be used to do very complex matrix manipulation. For example x = y(y > 0.45) finds all the elements of vector y such that yi > 0.45 and stores them in vector x. These operations can be combined with boolean operators, too.

 From 'Boolean Operator' to home

 From 'Boolean Operator' to 'Boolean Algebra Menu'

Boolean Algebra Laws

Logical AND

Logical gate OR

Logical XOR

Boolean NOT

footer for boolean operator page