<html lang="en"> <head> <title>Predicates for Numeric Objects - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Numeric-Data-Types.html#Numeric-Data-Types" title="Numeric Data Types"> <link rel="prev" href="Promotion-and-Demotion-of-Data-Types.html#Promotion-and-Demotion-of-Data-Types" title="Promotion and Demotion of Data Types"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Predicates-for-Numeric-Objects"></a> <p> Previous: <a rel="previous" accesskey="p" href="Promotion-and-Demotion-of-Data-Types.html#Promotion-and-Demotion-of-Data-Types">Promotion and Demotion of Data Types</a>, Up: <a rel="up" accesskey="u" href="Numeric-Data-Types.html#Numeric-Data-Types">Numeric Data Types</a> <hr> </div> <h3 class="section">4.8 Predicates for Numeric Objects</h3> <p>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 <code>abs</code> 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. <pre class="example"> function a = abs (x) if (isreal (x)) a = sign (x) .* x; elseif (iscomplex (x)) a = sqrt (real(x).^2 + imag(x).^2); endif endfunction </pre> <p>The following functions are available for determining the type of a variable. <!-- isnumeric src/data.cc --> <p><a name="doc_002disnumeric"></a> <div class="defun"> — Built-in Function: <b>isnumeric</b> (<var>x</var>)<var><a name="index-isnumeric-302"></a></var><br> <blockquote><p>Return true if <var>x</var> is a numeric object, i.e., an integer, real, or complex array. Logical and character arrays are not considered to be numeric. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002disinteger.html#doc_002disinteger">isinteger</a>, <a href="doc_002disfloat.html#doc_002disfloat">isfloat</a>, <a href="doc_002disreal.html#doc_002disreal">isreal</a>, <a href="doc_002discomplex.html#doc_002discomplex">iscomplex</a>, <a href="doc_002dislogical.html#doc_002dislogical">islogical</a>, <a href="doc_002dischar.html#doc_002dischar">ischar</a>, <a href="doc_002discell.html#doc_002discell">iscell</a>, <a href="doc_002disstruct.html#doc_002disstruct">isstruct</a>. </p></blockquote></div> <!-- isfloat src/data.cc --> <p><a name="doc_002disfloat"></a> <div class="defun"> — Built-in Function: <b>isfloat</b> (<var>x</var>)<var><a name="index-isfloat-303"></a></var><br> <blockquote><p>Return true if <var>x</var> is a floating-point numeric object. Objects of class double or single are floating-point objects. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002disinteger.html#doc_002disinteger">isinteger</a>, <a href="doc_002dischar.html#doc_002dischar">ischar</a>, <a href="doc_002dislogical.html#doc_002dislogical">islogical</a>, <a href="doc_002disnumeric.html#doc_002disnumeric">isnumeric</a>, <a href="doc_002disa.html#doc_002disa">isa</a>. </p></blockquote></div> <!-- isreal src/data.cc --> <p><a name="doc_002disreal"></a> <div class="defun"> — Built-in Function: <b>isreal</b> (<var>x</var>)<var><a name="index-isreal-304"></a></var><br> <blockquote><p>Return true if <var>x</var> is a non-complex matrix or scalar. For compatibility with <span class="sc">matlab</span>, this includes logical and character matrices. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002discomplex.html#doc_002discomplex">iscomplex</a>, <a href="doc_002disnumeric.html#doc_002disnumeric">isnumeric</a>. </p></blockquote></div> <!-- iscomplex src/data.cc --> <p><a name="doc_002discomplex"></a> <div class="defun"> — Built-in Function: <b>iscomplex</b> (<var>x</var>)<var><a name="index-iscomplex-305"></a></var><br> <blockquote><p>Return true if <var>x</var> is a complex-valued numeric object. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002disreal.html#doc_002disreal">isreal</a>, <a href="doc_002disnumeric.html#doc_002disnumeric">isnumeric</a>. </p></blockquote></div> <!-- ismatrix src/data.cc --> <p><a name="doc_002dismatrix"></a> <div class="defun"> — Built-in Function: <b>ismatrix</b> (<var>a</var>)<var><a name="index-ismatrix-306"></a></var><br> <blockquote><p>Return true if <var>a</var> 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 <code>ismatrix</code> will return true for these objects as well. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002disscalar.html#doc_002disscalar">isscalar</a>, <a href="doc_002disvector.html#doc_002disvector">isvector</a>, <a href="doc_002discell.html#doc_002discell">iscell</a>, <a href="doc_002disstruct.html#doc_002disstruct">isstruct</a>, <a href="doc_002dissparse.html#doc_002dissparse">issparse</a>. </p></blockquote></div> <!-- isvector scripts/general/isvector.m --> <p><a name="doc_002disvector"></a> <div class="defun"> — Function File: <b>isvector</b> (<var>x</var>)<var><a name="index-isvector-307"></a></var><br> <blockquote><p>Return true if <var>x</var> 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. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002disscalar.html#doc_002disscalar">isscalar</a>, <a href="doc_002dismatrix.html#doc_002dismatrix">ismatrix</a>, <a href="doc_002dsize.html#doc_002dsize">size</a>, <a href="doc_002drows.html#doc_002drows">rows</a>, <a href="doc_002dcolumns.html#doc_002dcolumns">columns</a>, <a href="doc_002dlength.html#doc_002dlength">length</a>. </p></blockquote></div> <!-- isrow scripts/general/isrow.m --> <p><a name="doc_002disrow"></a> <div class="defun"> — Function File: <b>isrow</b> (<var>x</var>)<var><a name="index-isrow-308"></a></var><br> <blockquote><p>Return true if <var>x</var> is a row vector. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002discolumn.html#doc_002discolumn">iscolumn</a>, <a href="doc_002disscalar.html#doc_002disscalar">isscalar</a>, <a href="doc_002disvector.html#doc_002disvector">isvector</a>, <a href="doc_002dismatrix.html#doc_002dismatrix">ismatrix</a>. </p></blockquote></div> <!-- iscolumn scripts/general/iscolumn.m --> <p><a name="doc_002discolumn"></a> <div class="defun"> — Function File: <b>iscolumn</b> (<var>x</var>)<var><a name="index-iscolumn-309"></a></var><br> <blockquote><p>Return true if <var>x</var> is a column vector. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002disrow.html#doc_002disrow">isrow</a>, <a href="doc_002disscalar.html#doc_002disscalar">isscalar</a>, <a href="doc_002disvector.html#doc_002disvector">isvector</a>, <a href="doc_002dismatrix.html#doc_002dismatrix">ismatrix</a>. </p></blockquote></div> <!-- isscalar scripts/general/isscalar.m --> <p><a name="doc_002disscalar"></a> <div class="defun"> — Function File: <b>isscalar</b> (<var>x</var>)<var><a name="index-isscalar-310"></a></var><br> <blockquote><p>Return true if <var>x</var> is a scalar. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002disvector.html#doc_002disvector">isvector</a>, <a href="doc_002dismatrix.html#doc_002dismatrix">ismatrix</a>. </p></blockquote></div> <!-- issquare scripts/general/issquare.m --> <p><a name="doc_002dissquare"></a> <div class="defun"> — Function File: <b>issquare</b> (<var>x</var>)<var><a name="index-issquare-311"></a></var><br> <blockquote><p>Return true if <var>x</var> is a square matrix. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002disscalar.html#doc_002disscalar">isscalar</a>, <a href="doc_002disvector.html#doc_002disvector">isvector</a>, <a href="doc_002dismatrix.html#doc_002dismatrix">ismatrix</a>, <a href="doc_002dsize.html#doc_002dsize">size</a>. </p></blockquote></div> <!-- issymmetric scripts/linear-algebra/issymmetric.m --> <p><a name="doc_002dissymmetric"></a> <div class="defun"> — Function File: <b>issymmetric</b> (<var>x</var>)<var><a name="index-issymmetric-312"></a></var><br> — Function File: <b>issymmetric</b> (<var>x, tol</var>)<var><a name="index-issymmetric-313"></a></var><br> <blockquote><p>Return true if <var>x</var> is a symmetric matrix within the tolerance specified by <var>tol</var>. The default tolerance is zero (uses faster code). Matrix <var>x</var> is considered symmetric if <code>norm (</code><var>x</var><code> - </code><var>x</var><code>.', Inf) / norm (</code><var>x</var><code>, Inf) < </code><var>tol</var>. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dishermitian.html#doc_002dishermitian">ishermitian</a>, <a href="doc_002disdefinite.html#doc_002disdefinite">isdefinite</a>. </p></blockquote></div> <!-- ishermitian scripts/linear-algebra/ishermitian.m --> <p><a name="doc_002dishermitian"></a> <div class="defun"> — Function File: <b>ishermitian</b> (<var>x</var>)<var><a name="index-ishermitian-314"></a></var><br> — Function File: <b>ishermitian</b> (<var>x, tol</var>)<var><a name="index-ishermitian-315"></a></var><br> <blockquote><p>Return true if <var>x</var> is Hermitian within the tolerance specified by <var>tol</var>. The default tolerance is zero (uses faster code). Matrix <var>x</var> is considered symmetric if <code>norm (</code><var>x</var><code> - </code><var>x</var><code>', Inf) / norm (</code><var>x</var><code>, Inf) < </code><var>tol</var>. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dissymmetric.html#doc_002dissymmetric">issymmetric</a>, <a href="doc_002disdefinite.html#doc_002disdefinite">isdefinite</a>. </p></blockquote></div> <!-- isdefinite scripts/linear-algebra/isdefinite.m --> <p><a name="doc_002disdefinite"></a> <div class="defun"> — Function File: <b>isdefinite</b> (<var>x</var>)<var><a name="index-isdefinite-316"></a></var><br> — Function File: <b>isdefinite</b> (<var>x, tol</var>)<var><a name="index-isdefinite-317"></a></var><br> <blockquote><p>Return 1 if <var>x</var> is symmetric positive definite within the tolerance specified by <var>tol</var> or 0 if <var>x</var> is symmetric positive semidefinite. Otherwise, return -1. If <var>tol</var> is omitted, use a tolerance of <code>100 * eps * norm (</code><var>x</var><code>, "fro")</code> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dissymmetric.html#doc_002dissymmetric">issymmetric</a>, <a href="doc_002dishermitian.html#doc_002dishermitian">ishermitian</a>. </p></blockquote></div> <!-- islogical src/data.cc --> <p><a name="doc_002dislogical"></a> <div class="defun"> — Built-in Function: <b>islogical</b> (<var>x</var>)<var><a name="index-islogical-318"></a></var><br> — Built-in Function: <b>isbool</b> (<var>x</var>)<var><a name="index-isbool-319"></a></var><br> <blockquote><p>Return true if <var>x</var> is a logical object. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002disfloat.html#doc_002disfloat">isfloat</a>, <a href="doc_002disinteger.html#doc_002disinteger">isinteger</a>, <a href="doc_002dischar.html#doc_002dischar">ischar</a>, <a href="doc_002disnumeric.html#doc_002disnumeric">isnumeric</a>, <a href="doc_002disa.html#doc_002disa">isa</a>. </p></blockquote></div> <!-- isprime scripts/specfun/isprime.m --> <p><a name="doc_002disprime"></a> <div class="defun"> — Function File: <b>isprime</b> (<var>x</var>)<var><a name="index-isprime-320"></a></var><br> <blockquote><p>Return a logical array which is true where the elements of <var>x</var> are prime numbers and false where they are not. <p>If the maximum value in <var>x</var> is very large, then you should be using special purpose factorization code. <pre class="example"> isprime (1:6) ⇒ [0, 1, 1, 0, 1, 0] </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dprimes.html#doc_002dprimes">primes</a>, <a href="doc_002dfactor.html#doc_002dfactor">factor</a>, <a href="doc_002dgcd.html#doc_002dgcd">gcd</a>, <a href="doc_002dlcm.html#doc_002dlcm">lcm</a>. </p></blockquote></div> <p>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 <a href="Status-of-Variables.html#Status-of-Variables">Status of Variables</a>. <!-- DO NOT EDIT! Generated automatically by munge-texi.pl. --> <!-- Copyright (C) 1996-2012 John W. Eaton --> <!-- This file is part of Octave. --> <!-- Octave is free software; you can redistribute it and/or modify it --> <!-- under the terms of the GNU General Public License as published by the --> <!-- Free Software Foundation; either version 3 of the License, or (at --> <!-- your option) any later version. --> <!-- Octave is distributed in the hope that it will be useful, but WITHOUT --> <!-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or --> <!-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --> <!-- for more details. --> <!-- You should have received a copy of the GNU General Public License --> <!-- along with Octave; see the file COPYING. If not, see --> <!-- <http://www.gnu.org/licenses/>. --> </body></html>