Next: Rearranging Matrices, Up: Matrix Manipulation

The functions `any`

and `all`

are useful for determining
whether any or all of the elements of a matrix satisfy some condition.
The `find`

function is also useful in determining which elements of
a matrix meet a specified condition.

— Built-in Function: **any** (`x, dim`)

For a vector argument, return 1 if any element of the vector is nonzero.

For a matrix argument, return a row vector of ones and zeros with each element indicating whether any of the elements of the corresponding column of the matrix are nonzero. For example,

any (eye (2, 4)) => [ 1, 1, 0, 0 ]If the optional argument

dimis supplied, work along dimensiondim. For example,any (eye (2, 4), 2) => [ 1; 1 ]

— Built-in Function: **all** (`x, dim`)

The function

`all`

behaves like the function`any`

, except that it returns true only if all the elements of a vector, or all the elements along dimensiondimof a matrix, are nonzero.

Since the comparison operators (see Comparison Ops) return matrices of ones and zeros, it is easy to test a matrix for many things, not just whether the elements are nonzero. For example,

all (all (rand (5) < 0.9)) => 0

tests a random 5 by 5 matrix to see if all of its elements are less than 0.9.

Note that in conditional contexts (like the test clause of `if`

and
`while`

statements) Octave treats the test as if you had typed
`all (all (condition))`

.

— Mapping Function: **xor** (`x, y`)

Return the `exclusive or' of the entries of

xandy. For boolean expressionsxandy,`xor (`

x`,`

y`)`

is true if and only ifxoryis true, but not if bothxandyare true.

— Function File: **is_duplicate_entry** (`x`)

Return non-zero if any entries in

xare duplicates of one another.

— Function File: **diff** (`x, k, dim`)

If

xis a vector of lengthn,`diff (`

x`)`

is the vector of first differencesx(2) -x(1), ...,x(n) -x(n-1).If

xis a matrix,`diff (`

x`)`

is the matrix of column differences along the first non-singleton dimension.The second argument is optional. If supplied,

`diff (`

x`,`

k`)`

, wherekis a nonnegative integer, returns thek-th differences. It is possible thatkis larger than then first non-singleton dimension of the matrix. In this case,`diff`

continues to take the differences along the next non-singleton dimension.The dimension along which to take the difference can be explicitly stated with the optional variable

dim. In this case thek-th order differences are calculated along this dimension. In the case wherekexceeds`size (`

x`,`

dim`)`

then an empty matrix is returned.

— Mapping Function: **isinf** (`x`)

Return 1 for elements of

xthat are infinite and zero otherwise. For example,isinf ([13, Inf, NA, NaN]) => [ 0, 1, 0, 0 ]

— Mapping Function: **isnan** (`x`)

Return 1 for elements of

xthat are NaN values and zero otherwise. For example,isnan ([13, Inf, NA, NaN]) => [ 0, 0, 0, 1 ]

— Mapping Function: **finite** (`x`)

Return 1 for elements of

xthat are finite values and zero otherwise. For example,finite ([13, Inf, NA, NaN]) => [ 1, 0, 0, 0 ]

— Loadable Function: **find** (`x`)

Return a vector of indices of nonzero elements of a matrix. To obtain a single index for each matrix element, Octave pretends that the columns of a matrix form one long vector (like Fortran arrays are stored). For example,

find (eye (2)) => [ 1; 4 ]If two outputs are requested,

`find`

returns the row and column indices of nonzero elements of a matrix. For example,[i, j] = find (2 * eye (2)) => i = [ 1; 2 ] => j = [ 1; 2 ]If three outputs are requested,

`find`

also returns a vector containing the nonzero values. For example,[i, j, v] = find (3 * eye (2)) => i = [ 1; 2 ] => j = [ 1; 2 ] => v = [ 3; 3 ]

— Function File: [`err`, `y1`, ...] = **common_size** (`x1, ...`)

Determine if all input arguments are either scalar or of common size. If so,

erris zero, andyiis a matrix of the common size with all entries equal toxiif this is a scalar orxiotherwise. If the inputs cannot be brought to a common size, errorcode is 1, andyiisxi. For example,[errorcode, a, b] = common_size ([1 2; 3 4], 5) => errorcode = 0 => a = [ 1, 2; 3, 4 ] => b = [ 5, 5; 5, 5 ]This is useful for implementing functions where arguments can either be scalars or of common size.