GNU Octave Manual Version 3by John W. Eaton, David Bateman, Søren Hauberg Paperback (6"x9"), 568 pages ISBN 095461206X RRP £24.95 ($39.95) |

## 4.5 Logical Values

Octave has built-in support for logical values, i.e. variables that
are either `true`

or `false`

. When comparing two variables,
the result will be a logical value whose value depends on whether or
not the comparison is true.

The basic logical operations are `&`

, `|`

, and `!`

,
which correspond to “Logical And”, “Logical Or”, and “Logical
Negation”. These operations all follow the usual rules of logic.

It is also possible to use logical values as part of standard numerical
calculations. In this case `true`

is converted to `1`

, and
`false`

to 0, both represented using double precision floating
point numbers. So, the result of `true*22 - false/6`

is `22`

.

Logical values can also be used to index matrices and cell arrays.
When indexing with a logical array the result will be a vector containing
the values corresponding to `true`

parts of the logical array.
The following example illustrates this.

data = [ 1, 2; 3, 4 ]; idx = (data <= 2); data(idx) => ans = [ 1; 4 ]

Instead of creating the `idx`

array it is possible to replace
`data(idx)`

with `data( data <= 2 )`

in the above code.

Logical values can also be constructed by
casting numeric objects to logical values, or by using the `true`

or `false`

functions.

__Function File:__**logical***(*`arg`)- Convert
`arg`to a logical value. For example,logical ([-1, 0, 1])

is equivalent to

[-1, 0, 1] != 0

__Built-in Function:__**true***(*`x`)__Built-in Function:__**true***(*`n`,`m`)__Built-in Function:__**true***(*`n`,`m`,`k`, ...)- Return a matrix or N-dimensional array whose elements are all logical 1.
The arguments are handled the same as the arguments for
`eye`

.

__Built-in Function:__**false***(*`x`)__Built-in Function:__**false***(*`n`,`m`)__Built-in Function:__**false***(*`n`,`m`,`k`, ...)- Return a matrix or N-dimensional array whose elements are all logical 0.
The arguments are handled the same as the arguments for
`eye`

.

ISBN 095461206X | GNU Octave Manual Version 3 | See the print edition |