Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 341

octave-doc-3.6.4-3.mga4.noarch.rpm

<html lang="en">
<head>
<title>Logical Values - 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="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:&nbsp;<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:&nbsp;<a rel="previous" accesskey="p" href="Bit-Manipulations.html#Bit-Manipulations">Bit Manipulations</a>,
Up:&nbsp;<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>&amp;</code>, <code>|</code>, and <code>!</code>,
which correspond to &ldquo;Logical And&rdquo;, &ldquo;Logical Or&rdquo;, and &ldquo;Logical
Negation&rdquo;.  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 &lt;= 2);
     data(idx)
          &rArr; 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(&nbsp;data&nbsp;&lt;=&nbsp;2&nbsp;)</code><!-- /@w --> 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.

<!-- logical src/ov-bool-mat.cc -->
   <p><a name="doc_002dlogical"></a>

<div class="defun">
&mdash; Built-in Function:  <b>logical</b> (<var>x</var>)<var><a name="index-logical-295"></a></var><br>
<blockquote><p>Convert <var>x</var> to logical type. 
<!-- 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_002ddouble.html#doc_002ddouble">double</a>, <a href="doc_002dsingle.html#doc_002dsingle">single</a>, <a href="doc_002dchar.html#doc_002dchar">char</a>. 
</p></blockquote></div>

<!-- true src/data.cc -->
   <p><a name="doc_002dtrue"></a>

<div class="defun">
&mdash; Built-in Function:  <b>true</b> (<var>x</var>)<var><a name="index-true-296"></a></var><br>
&mdash; Built-in Function:  <b>true</b> (<var>n, m</var>)<var><a name="index-true-297"></a></var><br>
&mdash; Built-in Function:  <b>true</b> (<var>n, m, k, <small class="dots">...</small></var>)<var><a name="index-true-298"></a></var><br>
<blockquote><p>Return a matrix or N-dimensional array whose elements are all logical 1. 
If invoked with a single scalar integer argument, return a square
matrix of the specified size.  If invoked with two or more scalar
integer arguments, or a vector of integer values, return an array with
given dimensions. 
<!-- 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_002dfalse.html#doc_002dfalse">false</a>. 
</p></blockquote></div>

<!-- false src/data.cc -->
   <p><a name="doc_002dfalse"></a>

<div class="defun">
&mdash; Built-in Function:  <b>false</b> (<var>x</var>)<var><a name="index-false-299"></a></var><br>
&mdash; Built-in Function:  <b>false</b> (<var>n, m</var>)<var><a name="index-false-300"></a></var><br>
&mdash; Built-in Function:  <b>false</b> (<var>n, m, k, <small class="dots">...</small></var>)<var><a name="index-false-301"></a></var><br>
<blockquote><p>Return a matrix or N-dimensional array whose elements are all logical 0. 
If invoked with a single scalar integer argument, return a square
matrix of the specified size.  If invoked with two or more scalar
integer arguments, or a vector of integer values, return an array with
given dimensions. 
<!-- 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_002dtrue.html#doc_002dtrue">true</a>. 
</p></blockquote></div>

   </body></html>