Next: , Up: Matrix Manipulation   [Contents][Index]


16.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)
Built-in Function: any (x, dim)

For a vector argument, return true (logical 1) if any element of the vector is nonzero.

For a matrix argument, return a row vector of logical 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 dim is supplied, work along dimension dim. For example:

any (eye (2, 4), 2)
 ⇒ [ 1; 1 ]

See also: all.

Built-in Function: all (x)
Built-in Function: all (x, dim)

For a vector argument, return true (logical 1) if all elements of the vector are nonzero.

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

all ([2, 3; 1, 0]))
    ⇒ [ 1, 0 ]

If the optional argument dim is supplied, work along dimension dim.

See also: any.

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: z = xor (x, y)

Return the exclusive or of the entries of x and y. For boolean expressions x and y, xor (x, y) is true if and only if one of x or y is true. Otherwise, for x and y both true or both false, xor returns false.

The truth table for the xor operation is

xyz
000
101
011
110

See also: and, or, not.

Built-in Function: diff (x)
Built-in Function: diff (x, k)
Built-in Function: diff (x, k, dim)

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 along the first non-singleton dimension.

The second argument is optional. If supplied, diff (x, k), where k is a non-negative integer, returns the k-th differences. It is possible that k is larger than the 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 the k-th order differences are calculated along this dimension. In the case where k exceeds size (x, dim) an empty matrix is returned.

See also: sort, merge.

Mapping Function: isinf (x)

Return a logical array which is true where the elements of x are are infinite and false where they are not. For example:

isinf ([13, Inf, NA, NaN])
      ⇒ [ 0, 1, 0, 0 ]

See also: isfinite, isnan, isna.

Mapping Function: isnan (x)

Return a logical array which is true where the elements of x are NaN values and false where they are not. NA values are also considered NaN values. For example:

isnan ([13, Inf, NA, NaN])
      ⇒ [ 0, 0, 1, 1 ]

See also: isna, isinf, isfinite.

Mapping Function: isfinite (x)
Mapping Function: finite (x)

Return a logical array which is true where the elements of x are finite values and false where they are not. For example:

finite ([13, Inf, NA, NaN])
     ⇒ [ 1, 0, 0, 0 ]

See also: isinf, isnan, isna.

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, err 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.

Built-in Function: idx = find (x)
Built-in Function: idx = find (x, n)
Built-in Function: idx = find (x, n, direction)
Built-in Function: [i, j] = find (…)
Built-in Function: [i, j, v] = find (…)

Return a vector of indices of nonzero elements of a matrix, as a row if x is a row vector or as a column otherwise. 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 ]

If two inputs are given, n indicates the maximum number of elements to find from the beginning of the matrix or vector.

If three inputs are given, direction should be one of "first" or "last", requesting only the first or last n indices, respectively. However, the indices are always returned in ascending order.

Note that this function is particularly useful for sparse matrices, as it extracts the non-zero elements as vectors, which can then be used to create the original matrix. For example:

sz = size (a);
[i, j, v] = find (a);
b = sparse (i, j, v, sz(1), sz(2));

See also: nonzeros.

Built-in Function: idx = lookup (table, y)
Built-in Function: idx = lookup (table, y, opt)

Lookup values in a sorted table. Usually used as a prelude to interpolation.

If table is increasing and idx = lookup (table, y), then table(idx(i)) <= y(i) < table(idx(i+1)) for all y(i) within the table. If y(i) < table(1) then idx(i) is 0. If y(i) >= table(end) or isnan (y(i)) then idx(i) is n.

If the table is decreasing, then the tests are reversed. For non-strictly monotonic tables, empty intervals are always skipped. The result is undefined if table is not monotonic, or if table contains a NaN.

The complexity of the lookup is O(M*log(N)) where N is the size of table and M is the size of y. In the special case when y is also sorted, the complexity is O(min(M*log(N),M+N)).

table and y can also be cell arrays of strings (or y can be a single string). In this case, string lookup is performed using lexicographical comparison.

If opts is specified, it must be a string with letters indicating additional options.

m

table(idx(i)) == val(i) if val(i) occurs in table; otherwise, idx(i) is zero.

b

idx(i) is a logical 1 or 0, indicating whether val(i) is contained in table or not.

l

For numeric lookups the leftmost subinterval shall be extended to infinity (i.e., all indices at least 1)

r

For numeric lookups the rightmost subinterval shall be extended to infinity (i.e., all indices at most n-1).

If you wish to check if a variable exists at all, instead of properties its elements may have, consult Status of Variables.


Next: , Up: Matrix Manipulation   [Contents][Index]