<html lang="en"> <head> <title>Logical Values - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <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="Bit-Manipulations.html#Bit-Manipulations" title="Bit Manipulations"> <link rel="next" 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="Logical-Values"></a> <p> Next: <a rel="next" accesskey="n" href="Promotion-and-Demotion-of-Data-Types.html#Promotion-and-Demotion-of-Data-Types">Promotion and Demotion of Data Types</a>, Previous: <a rel="previous" accesskey="p" href="Bit-Manipulations.html#Bit-Manipulations">Bit Manipulations</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.6 Logical Values</h3> <p>Octave has built-in support for logical values, i.e., variables that are either <code>true</code> or <code>false</code>. When comparing two variables, the result will be a logical value whose value depends on whether or not the comparison is true. <p>The basic logical operations are <code>&</code>, <code>|</code>, and <code>!</code>, which correspond to “Logical And”, “Logical Or”, and “Logical Negation”. These operations all follow the usual rules of logic. <p>It is also possible to use logical values as part of standard numerical calculations. In this case <code>true</code> is converted to <code>1</code>, and <code>false</code> to 0, both represented using double precision floating point numbers. So, the result of <code>true*22 - false/6</code> is <code>22</code>. <p>Logical values can also be used to index matrices and cell arrays. When indexing with a logical array the result will be a vector containing the values corresponding to <code>true</code> parts of the logical array. The following example illustrates this. <pre class="example"> data = [ 1, 2; 3, 4 ]; idx = (data <= 2); data(idx) ⇒ ans = [ 1; 2 ] </pre> <p class="noindent">Instead of creating the <code>idx</code> array it is possible to replace <code>data(idx)</code> with <code>data( data <= 2 )</code> in the above code. <p>Logical values can also be constructed by casting numeric objects to logical values, or by using the <code>true</code> or <code>false</code> functions. <!-- ./general/logical.m --> <p><a name="doc_002dlogical"></a> <div class="defun"> — Function File: <b>logical</b> (<var>arg</var>)<var><a name="index-logical-260"></a></var><br> <blockquote><p>Convert <var>arg</var> to a logical value. For example, <pre class="example"> logical ([-1, 0, 1]) </pre> <p class="noindent">is equivalent to <pre class="example"> [-1, 0, 1] != 0 </pre> </blockquote></div> <!-- data.cc --> <p><a name="doc_002dtrue"></a> <div class="defun"> — Built-in Function: <b>true</b> (<var>x</var>)<var><a name="index-true-261"></a></var><br> — Built-in Function: <b>true</b> (<var>n, m</var>)<var><a name="index-true-262"></a></var><br> — Built-in Function: <b>true</b> (<var>n, m, k, <small class="dots">...</small></var>)<var><a name="index-true-263"></a></var><br> <blockquote><p>Return a matrix or N-dimensional array whose elements are all logical 1. The arguments are handled the same as the arguments for <code>eye</code>. </p></blockquote></div> <!-- data.cc --> <p><a name="doc_002dfalse"></a> <div class="defun"> — Built-in Function: <b>false</b> (<var>x</var>)<var><a name="index-false-264"></a></var><br> — Built-in Function: <b>false</b> (<var>n, m</var>)<var><a name="index-false-265"></a></var><br> — Built-in Function: <b>false</b> (<var>n, m, k, <small class="dots">...</small></var>)<var><a name="index-false-266"></a></var><br> <blockquote><p>Return a matrix or N-dimensional array whose elements are all logical 0. The arguments are handled the same as the arguments for <code>eye</code>. </p></blockquote></div> </body></html>