- publishing free software manuals
GNU Octave Manual
by John W. Eaton
Paperback (6"x9"), 324 pages, 4 figures
ISBN 0954161726
RRP £19.99 ($29.99)

Get a printed copy>>>

15.1 Finding Elements and Checking Conditions

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)
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 ]

To see if any of the elements of a matrix are nonzero, you can use a statement like

any (any (a))

Built-in Function: all (x)
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 in a column of a matrix, are nonzero.

Since the comparison operators (see section 8.4 Comparison Operators) 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)).

Function File: [err, y1, ...] = common_size (x1, ...)
Determine if all input arguments are either scalar or of common size. If so, err is zero, and yi is a matrix of the common size with all entries equal to xi if this is a scalar or xi otherwise. If the inputs cannot be brought to a common size, errorcode is 1, and yi is xi. 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.

Function File: diff (x, k)
If x is a vector of length n, diff (x) is the vector of first differences x(2) - x(1), ..., x(n) - x(n-1).

If x is a matrix, diff (x) is the matrix of column differences.

The second argument is optional. If supplied, diff (x, k), where k is a nonnegative integer, returns the k-th differences.

Mapping Function: isinf (x)
Return 1 for elements of x that are infinite and zero otherwise. For example,

isinf ([13, Inf, NaN])
     => [ 0, 1, 0 ]

Mapping Function: isnan (x)
Return 1 for elements of x that are NaN values and zero otherwise. For example,

isnan ([13, Inf, NaN])
     => [ 0, 0, 1 ]

Mapping Function: finite (x)
Return 1 for elements of x that are NaN values and zero otherwise. For example,

finite ([13, Inf, NaN])
     => [ 1, 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 ]

ISBN 0954161726GNU Octave ManualSee the print edition