next up previous contents
Next: MINUS Subtraction Operator Up: Mathematical Operators Previous: LEFTDIVIDE Matrix Equation Solver/Divide   Contents

Subsections

LOGICALOPS Logical Array Operators

Usage

There are three Boolean operators available in FreeMat. The syntax for their use is:

  y = ~x
  y = a & b
  y = a | b

where x, a and b are logical arrays. The operators are

The binary operators AND and OR can take scalar arguments as well as vector arguments, in which case, the scalar is operated on with each element of the vector.

Examples

Some simple examples of logical operators. Suppose we want to calculate the exclusive-or (XOR) of two vectors of logical variables. First, we create a pair of vectors to perform the XOR operation on:

--> a = (randn(1,6)>0)
a = 
  <logical>  - size: [1 6]
 
Columns 1 to 6
 0  0  1  0  1  1  
--> b = (randn(1,6)>0)
b = 
  <logical>  - size: [1 6]
 
Columns 1 to 6
 0  0  0  0  1  1

Next, we can compute the OR of a and b:

--> c = a | b
c = 
  <logical>  - size: [1 6]
 
Columns 1 to 6
 0  0  1  0  1  1

However, the XOR and OR operations differ on the fifth entry - the XOR would be false, since it is true if and only if exactly one of the two inputs is true. To isolate this case, we can AND the two vectors, to find exactly those entries that appear as true in both a and b:

--> d = a & b
d = 
  <logical>  - size: [1 6]
 
Columns 1 to 6
 0  0  0  0  1  1

At this point, we can modify the contents of c in two ways - the Boolean way is to AND \sim d with c, like so

--> xor = c & (~d)
xor = 
  <logical>  - size: [1 6]
 
Columns 1 to 6
 0  0  1  0  0  0

The other way to do this is simply force c(d) = 0, which uses the logical indexing mode of FreeMat (see the chapter on indexing for more details). This, however, will cause c to become an int32 type, as opposed to a logical type.

--> c(d) = 0
c = 
  <int32>  - size: [1 6]
 
Columns 1 to 5
             0              0              1              0              0  
 
Columns 6 to 6
             0


next up previous contents
Next: MINUS Subtraction Operator Up: Mathematical Operators Previous: LEFTDIVIDE Matrix Equation Solver/Divide   Contents
2004-08-26