octave: Finding Elements and Checking Conditions

 
 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: Seeall XREFall.
 
  -- : 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: Seeany XREFany.
 
    Since the comparison operators (SeeComparison 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: Seeand XREFand, Seeor XREFor, Seenot XREFnot.
 
  -- : 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 (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: Seesort XREFsort, Seemerge XREFmerge.
 
  -- : 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: Seeisfinite XREFisfinite, Seeisnan XREFisnan,
      Seeisna XREFisna.
 
  -- : 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 ]
 
DONTPRINTYET       See also: Seeisna XREFisna, Seeisinf XREFisinf, *noteDONTPRINTYET       See also: Seeisna XREFisna, Seeisinf XREFisinf, See
      isfinite XREFisfinite.
 
  -- : 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 ]
 
DONTPRINTYET       See also: Seeisinf XREFisinf, Seeisnan XREFisnan, *noteDONTPRINTYET       See also: Seeisinf XREFisinf, Seeisnan XREFisnan, See
      isna XREFisna.
 
  -- : [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: Seesize XREFsize, Seesize_equal XREFsize_equal,
      Seenumel XREFnumel, Seendims XREFndims.
 
  -- : 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: Seenonzeros XREFnonzeros.
 
  -- : 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 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)) == y(i)’ if ‘y(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 SeeStatus of Variables.