Previous: , Up: Numeric Data Types   [Contents][Index]


4.8 Predicates for Numeric Objects

Since the type of a variable may change during the execution of a program, it can be necessary to do type checking at run-time. Doing this also allows you to change the behavior of a function depending on the type of the input. As an example, this naive implementation of abs returns the absolute value of the input if it is a real number, and the length of the input if it is a complex number.

function a = abs (x)
  if (isreal (x))
    a = sign (x) .* x;
  elseif (iscomplex (x))
    a = sqrt (real(x).^2 + imag(x).^2);
  endif
endfunction

The following functions are available for determining the type of a variable.

Built-in Function: isnumeric (x)

Return true if x is a numeric object, i.e., an integer, real, or complex array. Logical and character arrays are not considered to be numeric.

See also: isinteger, isfloat, isreal, iscomplex, islogical, ischar, iscell, isstruct, isa.

Built-in Function: isfloat (x)

Return true if x is a floating-point numeric object. Objects of class double or single are floating-point objects.

See also: isinteger, ischar, islogical, isnumeric, isa.

Built-in Function: isreal (x)

Return true if x is a non-complex matrix or scalar. For compatibility with MATLAB, this includes logical and character matrices.

See also: iscomplex, isnumeric, isa.

Built-in Function: iscomplex (x)

Return true if x is a complex-valued numeric object.

See also: isreal, isnumeric, islogical, ischar, isfloat, isa.

Built-in Function: ismatrix (a)

Return true if a is a numeric, logical, or character matrix. Scalars (1x1 matrices) and vectors (1xN or Nx1 matrices) are subsets of the more general N-dimensional matrix and ismatrix will return true for these objects as well.

See also: isscalar, isvector, iscell, isstruct, issparse, isa.

Function File: isvector (x)

Return true if x is a vector. A vector is a 2-D array where one of the dimensions is equal to 1. As a consequence a 1x1 array, or scalar, is also a vector.

See also: isscalar, ismatrix, size, rows, columns, length.

Function File: isrow (x)

Return true if x is a row vector.

See also: iscolumn, isscalar, isvector, ismatrix.

Function File: iscolumn (x)

Return true if x is a column vector.

See also: isrow, isscalar, isvector, ismatrix.

Function File: isscalar (x)

Return true if x is a scalar.

See also: isvector, ismatrix.

Function File: issquare (x)

Return true if x is a square matrix.

See also: isscalar, isvector, ismatrix, size.

Function File: issymmetric (x)
Function File: issymmetric (x, tol)

Return true if x is a symmetric matrix within the tolerance specified by tol. The default tolerance is zero (uses faster code). Matrix x is considered symmetric if norm (x - x.', Inf) / norm (x, Inf) < tol.

See also: ishermitian, isdefinite.

Function File: ishermitian (x)
Function File: ishermitian (x, tol)

Return true if x is Hermitian within the tolerance specified by tol. The default tolerance is zero (uses faster code). Matrix x is considered symmetric if norm (x - x', Inf) / norm (x, Inf) < tol.

See also: issymmetric, isdefinite.

Function File: isdefinite (x)
Function File: isdefinite (x, tol)

Return 1 if x is symmetric positive definite within the tolerance specified by tol or 0 if x is symmetric positive semidefinite. Otherwise, return -1. If tol is omitted, use a tolerance of 100 * eps * norm (x, "fro")

See also: issymmetric, ishermitian.

Built-in Function: islogical (x)
Built-in Function: isbool (x)

Return true if x is a logical object.

See also: isfloat, isinteger, ischar, isnumeric, isa.

Function File: isprime (x)

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

If the maximum value in x is very large, then you should be using special purpose factorization code.

isprime (1:6)
    ⇒ [0, 1, 1, 0, 1, 0]

See also: primes, factor, gcd, lcm.

If instead of knowing properties of variables, you wish to know which variables are defined and to gather other information about the workspace itself, see Status of Variables.


Previous: , Up: Numeric Data Types   [Contents][Index]