all are useful for determining
whether any or all of the elements of a matrix satisfy some condition.
find function is also useful in determining which elements of
a matrix meet a specified condition.
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.
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
while statements) Octave treats the test as if you had typed
all (all (condition)).
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
xor returns false.
The truth table for the xor operation is
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.
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
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.
Return a logical array which is true where the elements of x are infinite and false where they are not.
isinf ([13, Inf, NA, NaN]) ⇒ [ 0, 1, 0, 0 ]
See also: isfinite, isnan, isna.
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.
Return a logical array which is true where the elements of x are finite values and false where they are not.
isfinite ([13, Inf, NA, NaN]) ⇒ [ 1, 0, 0, 0 ]
See also: isinf, isnan, isna.
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.
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
"last", requesting only the first or last
n indices, respectively. However, the indices are always returned in
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.
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
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.
table(idx(i)) == y(i) if
y(i) occurs in table;
idx(i) is zero.
idx(i) is a logical 1 or 0, indicating whether
is contained in table or not.
Left. For numeric lookups the leftmost subinterval shall be extended to minus infinity (i.e., all indices at least 1).
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
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.