logo for matrixlab-examples.com
[?] Subscribe To This Site

XML RSS
Add to Google
Add to My Yahoo!
Add to My MSN
Subscribe with Bloglines


Home
Matrixmania Blog
Contact
-> Sitemap <-
Matlab Books
Quick Matlab Guide
Matlab Tutorials
Matlab Examples
Matlab Flow Control
Boolean Algebra
Linear Algebra
Matlab 2D Plots
Matlab 3D Plots
Matlab GUI
Matlab Cookbook I
Matlab Cookbook II
Probability and Stats
Forums and Help
Relevant Links
Fun!
Your own Website?
Terms/Policies
leftimage for matrixlab-examples.com

Boolean Operator

In Matlab, there are four logical (aka 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.

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:
x<y                    
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

 

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.

 

footer for boolean operator page