<html lang="en"> <head> <title>Finding Elements and Checking Conditions - 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="Matrix-Manipulation.html#Matrix-Manipulation" title="Matrix Manipulation"> <link rel="next" href="Rearranging-Matrices.html#Rearranging-Matrices" title="Rearranging Matrices"> <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="Finding-Elements-and-Checking-Conditions"></a> <p> Next: <a rel="next" accesskey="n" href="Rearranging-Matrices.html#Rearranging-Matrices">Rearranging Matrices</a>, Up: <a rel="up" accesskey="u" href="Matrix-Manipulation.html#Matrix-Manipulation">Matrix Manipulation</a> <hr> </div> <h3 class="section">16.1 Finding Elements and Checking Conditions</h3> <p>The functions <code>any</code> and <code>all</code> are useful for determining whether any or all of the elements of a matrix satisfy some condition. The <code>find</code> function is also useful in determining which elements of a matrix meet a specified condition. <!-- any src/data.cc --> <p><a name="doc_002dany"></a> <div class="defun"> — Built-in Function: <b>any</b> (<var>x</var>)<var><a name="index-any-1606"></a></var><br> — Built-in Function: <b>any</b> (<var>x, dim</var>)<var><a name="index-any-1607"></a></var><br> <blockquote><p>For a vector argument, return true (logical 1) if any element of the vector is nonzero. <p>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: <pre class="example"> any (eye (2, 4)) ⇒ [ 1, 1, 0, 0 ] </pre> <p>If the optional argument <var>dim</var> is supplied, work along dimension <var>dim</var>. For example: <pre class="example"> any (eye (2, 4), 2) ⇒ [ 1; 1 ] </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_002dall.html#doc_002dall">all</a>. </p></blockquote></div> <!-- all src/data.cc --> <p><a name="doc_002dall"></a> <div class="defun"> — Built-in Function: <b>all</b> (<var>x</var>)<var><a name="index-all-1608"></a></var><br> — Built-in Function: <b>all</b> (<var>x, dim</var>)<var><a name="index-all-1609"></a></var><br> <blockquote><p>For a vector argument, return true (logical 1) if all elements of the vector are nonzero. <p>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: <pre class="example"> all ([2, 3; 1, 0])) ⇒ [ 1, 0 ] </pre> <p>If the optional argument <var>dim</var> is supplied, work along dimension <var>dim</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_002dany.html#doc_002dany">any</a>. </p></blockquote></div> <p>Since the comparison operators (see <a href="Comparison-Ops.html#Comparison-Ops">Comparison Ops</a>) 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, <pre class="example"> all (all (rand (5) < 0.9)) ⇒ 0 </pre> <p class="noindent">tests a random 5 by 5 matrix to see if all of its elements are less than 0.9. <p>Note that in conditional contexts (like the test clause of <code>if</code> and <code>while</code> statements) Octave treats the test as if you had typed <code>all (all (condition))</code>. <!-- xor scripts/miscellaneous/xor.m --> <p><a name="doc_002dxor"></a> <div class="defun"> — Mapping Function: <var>z</var> = <b>xor</b> (<var>x, y</var>)<var><a name="index-xor-1610"></a></var><br> <blockquote><p>Return the `exclusive or' of the entries of <var>x</var> and <var>y</var>. For boolean expressions <var>x</var> and <var>y</var>, <code>xor (</code><var>x</var><code>, </code><var>y</var><code>)</code> is true if and only if one of <var>x</var> or <var>y</var> is true. Otherwise, for <var>x</var> and <var>y</var> both true or both false, <code>xor</code> returns false. <p>The truth table for the xor operation is <p><table summary=""><tr align="left"><td valign="top" width="44%"></td><td valign="top" width="3%"><var>x</var> </td><td valign="top" width="5%"><var>y</var> </td><td valign="top" width="3%"><var>z</var> </td><td valign="top" width="44%"> <br></td></tr><tr align="left"><td valign="top" width="44%"></td><td valign="top" width="3%">0 </td><td valign="top" width="5%">0 </td><td valign="top" width="3%">0 </td><td valign="top" width="44%"> <br></td></tr><tr align="left"><td valign="top" width="44%"></td><td valign="top" width="3%">1 </td><td valign="top" width="5%">0 </td><td valign="top" width="3%">1 </td><td valign="top" width="44%"> <br></td></tr><tr align="left"><td valign="top" width="44%"></td><td valign="top" width="3%">0 </td><td valign="top" width="5%">1 </td><td valign="top" width="3%">1 </td><td valign="top" width="44%"> <br></td></tr><tr align="left"><td valign="top" width="44%"></td><td valign="top" width="3%">1 </td><td valign="top" width="5%">1 </td><td valign="top" width="3%">0 </td><td valign="top" width="44%"> <br></td></tr></table> <!-- 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_002dand.html#doc_002dand">and</a>, <a href="doc_002dor.html#doc_002dor">or</a>, <a href="doc_002dnot.html#doc_002dnot">not</a>. </p></blockquote></div> <!-- diff src/data.cc --> <p><a name="doc_002ddiff"></a> <div class="defun"> — Built-in Function: <b>diff</b> (<var>x</var>)<var><a name="index-diff-1611"></a></var><br> — Built-in Function: <b>diff</b> (<var>x, k</var>)<var><a name="index-diff-1612"></a></var><br> — Built-in Function: <b>diff</b> (<var>x, k, dim</var>)<var><a name="index-diff-1613"></a></var><br> <blockquote><p>If <var>x</var> is a vector of length n, <code>diff (</code><var>x</var><code>)</code> is the vector of first differences <var>x</var>(2) - <var>x</var>(1), <small class="dots">...</small>, <var>x</var>(n) - <var>x</var>(n-1). <p>If <var>x</var> is a matrix, <code>diff (</code><var>x</var><code>)</code> is the matrix of column differences along the first non-singleton dimension. <p>The second argument is optional. If supplied, <code>diff (</code><var>x</var><code>, </code><var>k</var><code>)</code>, where <var>k</var> is a non-negative integer, returns the <var>k</var>-th differences. It is possible that <var>k</var> is larger than the first non-singleton dimension of the matrix. In this case, <code>diff</code> continues to take the differences along the next non-singleton dimension. <p>The dimension along which to take the difference can be explicitly stated with the optional variable <var>dim</var>. In this case the <var>k</var>-th order differences are calculated along this dimension. In the case where <var>k</var> exceeds <code>size (</code><var>x</var><code>, </code><var>dim</var><code>)</code> an empty matrix is returned. </p></blockquote></div> <!-- isinf src/mappers.cc --> <p><a name="doc_002disinf"></a> <div class="defun"> — Mapping Function: <b>isinf</b> (<var>x</var>)<var><a name="index-isinf-1614"></a></var><br> <blockquote><p>Return a logical array which is true where the elements of <var>x</var> are are infinite and false where they are not. For example: <pre class="example"> isinf ([13, Inf, NA, NaN]) ⇒ [ 0, 1, 0, 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_002disfinite.html#doc_002disfinite">isfinite</a>, <a href="doc_002disnan.html#doc_002disnan">isnan</a>, <a href="doc_002disna.html#doc_002disna">isna</a>. </p></blockquote></div> <!-- isnan src/mappers.cc --> <p><a name="doc_002disnan"></a> <div class="defun"> — Mapping Function: <b>isnan</b> (<var>x</var>)<var><a name="index-isnan-1615"></a></var><br> <blockquote><p>Return a logical array which is true where the elements of <var>x</var> are NaN values and false where they are not. NA values are also considered NaN values. For example: <pre class="example"> isnan ([13, Inf, NA, NaN]) ⇒ [ 0, 0, 1, 1 ] </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_002disna.html#doc_002disna">isna</a>, <a href="doc_002disinf.html#doc_002disinf">isinf</a>, <a href="doc_002disfinite.html#doc_002disfinite">isfinite</a>. </p></blockquote></div> <!-- isfinite src/mappers.cc --> <p><a name="doc_002disfinite"></a> <div class="defun"> — Mapping Function: <b>isfinite</b> (<var>x</var>)<var><a name="index-isfinite-1616"></a></var><br> — Mapping Function: <b>finite</b> (<var>x</var>)<var><a name="index-finite-1617"></a></var><br> <blockquote><p>Return a logical array which is true where the elements of <var>x</var> are finite values and false where they are not. For example: <pre class="example"> finite ([13, Inf, NA, NaN]) ⇒ [ 1, 0, 0, 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_002disinf.html#doc_002disinf">isinf</a>, <a href="doc_002disnan.html#doc_002disnan">isnan</a>, <a href="doc_002disna.html#doc_002disna">isna</a>. </p></blockquote></div> <!-- common_size scripts/general/common_size.m --> <p><a name="doc_002dcommon_005fsize"></a> <div class="defun"> — Function File: [<var>err</var>, <var>y1</var>, <small class="dots">...</small>] = <b>common_size</b> (<var>x1, <small class="dots">...</small></var>)<var><a name="index-common_005fsize-1618"></a></var><br> <blockquote><p>Determine if all input arguments are either scalar or of common size. If so, <var>err</var> is zero, and <var>yi</var> is a matrix of the common size with all entries equal to <var>xi</var> if this is a scalar or <var>xi</var> otherwise. If the inputs cannot be brought to a common size, <var>err</var> is 1, and <var>yi</var> is <var>xi</var>. For example: <pre class="example"> [errorcode, a, b] = common_size ([1 2; 3 4], 5) ⇒ errorcode = 0 ⇒ a = [ 1, 2; 3, 4 ] ⇒ b = [ 5, 5; 5, 5 ] </pre> <p class="noindent">This is useful for implementing functions where arguments can either be scalars or of common size. </p></blockquote></div> <!-- find src/DLD-FUNCTIONS/find.cc --> <p><a name="doc_002dfind"></a> <div class="defun"> — Loadable Function: <var>idx</var> = <b>find</b> (<var>x</var>)<var><a name="index-find-1619"></a></var><br> — Loadable Function: <var>idx</var> = <b>find</b> (<var>x, n</var>)<var><a name="index-find-1620"></a></var><br> — Loadable Function: <var>idx</var> = <b>find</b> (<var>x, n, direction</var>)<var><a name="index-find-1621"></a></var><br> — Loadable Function: [i, j] = <b>find</b> (<var><small class="dots">...</small></var>)<var><a name="index-find-1622"></a></var><br> — Loadable Function: [i, j, v] = <b>find</b> (<var><small class="dots">...</small></var>)<var><a name="index-find-1623"></a></var><br> <blockquote><p>Return a vector of indices of nonzero elements of a matrix, as a row if <var>x</var> 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: <pre class="example"> find (eye (2)) ⇒ [ 1; 4 ] </pre> <p>If two outputs are requested, <code>find</code> returns the row and column indices of nonzero elements of a matrix. For example: <pre class="example"> [i, j] = find (2 * eye (2)) ⇒ i = [ 1; 2 ] ⇒ j = [ 1; 2 ] </pre> <p>If three outputs are requested, <code>find</code> also returns a vector containing the nonzero values. For example: <pre class="example"> [i, j, v] = find (3 * eye (2)) ⇒ i = [ 1; 2 ] ⇒ j = [ 1; 2 ] ⇒ v = [ 3; 3 ] </pre> <p>If two inputs are given, <var>n</var> indicates the maximum number of elements to find from the beginning of the matrix or vector. <p>If three inputs are given, <var>direction</var> should be one of "first" or "last", requesting only the first or last <var>n</var> indices, respectively. However, the indices are always returned in ascending order. <p>Note that this function is particularly useful for sparse matrices, as it extracts the non-zero elements as vectors, which can then be used to create the original matrix. For example: <pre class="example"> sz = size (a); [i, j, v] = find (a); b = sparse (i, j, v, sz(1), sz(2)); </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_002dnonzeros.html#doc_002dnonzeros">nonzeros</a>. </p></blockquote></div> <!-- lookup src/DLD-FUNCTIONS/lookup.cc --> <p><a name="doc_002dlookup"></a> <div class="defun"> — Loadable Function: <var>idx</var> = <b>lookup</b> (<var>table, y</var>)<var><a name="index-lookup-1624"></a></var><br> — Loadable Function: <var>idx</var> = <b>lookup</b> (<var>table, y, opt</var>)<var><a name="index-lookup-1625"></a></var><br> <blockquote><p>Lookup values in a sorted table. Usually used as a prelude to interpolation. <p>If table is increasing and <code>idx = lookup (table, y)</code>, then <code>table(idx(i)) <= y(i) < table(idx(i+1))</code> for all <code>y(i)</code> within the table. If <code>y(i) < table(1)</code> then <code>idx(i)</code> is 0. If <code>y(i) >= table(end)</code> or <code>isnan (y(i))</code> then <code>idx(i)</code> is <code>n</code>. <p>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 <var>table</var> is not monotonic, or if <var>table</var> contains a NaN. <p>The complexity of the lookup is O(M*log(N)) where N is the size of <var>table</var> and M is the size of <var>y</var>. In the special case when <var>y</var> is also sorted, the complexity is O(min(M*log(N),M+N)). <p><var>table</var> and <var>y</var> can also be cell arrays of strings (or <var>y</var> can be a single string). In this case, string lookup is performed using lexicographical comparison. <p>If <var>opts</var> is specified, it must be a string with letters indicating additional options. <dl> <dt><code>m</code><dd><code>table(idx(i)) == val(i)</code> if <code>val(i)</code> occurs in table; otherwise, <code>idx(i)</code> is zero. <br><dt><code>b</code><dd><code>idx(i)</code> is a logical 1 or 0, indicating whether <code>val(i)</code> is contained in table or not. <br><dt><code>l</code><dd>For numeric lookups the leftmost subinterval shall be extended to infinity (i.e., all indices at least 1) <br><dt><code>r</code><dd>For numeric lookups the rightmost subinterval shall be extended to infinity (i.e., all indices at most n-1). </dl> </p></blockquote></div> <p>If you wish to check if a variable exists at all, instead of properties its elements may have, consult <a href="Status-of-Variables.html#Status-of-Variables">Status of Variables</a>. </body></html>