W3cubDocs

/Octave

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.

any (x)
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.

all (x)
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)).

z = xor (x, y)
z = xor (x1, x2, …)

Return the exclusive or 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, if x and y are both true or both false, xor returns false.

The truth table for the xor operation is

x y z
- - -
0 0 0
1 0 1
0 1 1
1 1 0

If more than two arguments are given the xor operation is applied cumulatively from left to right:

(…((x1 XOR x2) XOR x3) XOR …)

See also: and, or, not.

diff (x)
diff (x, k)
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 (xk), 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 (xdim) an empty matrix is returned.

See also: sort, merge.

isinf (x)

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

For example:

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

See also: isfinite, isnan, isna.

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.

isfinite (x)

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

For example:

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

See also: isinf, isnan, isna.

[err, yi, …] = common_size (xi, …)

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

If true, 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:

[err, a, b] = common_size ([1 2; 3 4], 5)
     ⇒ err = 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.

See also: size, size_equal, numel, ndims.

idx = find (x)
idx = find (x, n)
idx = find (x, n, direction)
[i, j] = find (…)
[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 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.

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 ]

Note that this function is particularly useful for sparse matrices, as it extracts the nonzero 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.

idx = lookup (table, y)
idx = lookup (table, y, opt)

Lookup values in a sorted table.

This function is usually used as a prelude to interpolation.

If table is increasing, of length N 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 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

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

b

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

l

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

r

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

Note: If table is not sorted the results from lookup will be unpredictable.

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

© 1996–2018 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/interpreter/Finding-Elements-and-Checking-Conditions.html