<html lang="en"> <head> <title>Element-by-element Boolean Operators - 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="Boolean-Expressions.html#Boolean-Expressions" title="Boolean Expressions"> <link rel="next" href="Short_002dcircuit-Boolean-Operators.html#Short_002dcircuit-Boolean-Operators" title="Short-circuit Boolean Operators"> <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="Element-by-element-Boolean-Operators"></a> <a name="Element_002dby_002delement-Boolean-Operators"></a> <p> Next: <a rel="next" accesskey="n" href="Short_002dcircuit-Boolean-Operators.html#Short_002dcircuit-Boolean-Operators">Short-circuit Boolean Operators</a>, Up: <a rel="up" accesskey="u" href="Boolean-Expressions.html#Boolean-Expressions">Boolean Expressions</a> <hr> </div> <h4 class="subsection">8.5.1 Element-by-element Boolean Operators</h4> <p><a name="index-element_002dby_002delement-evaluation-638"></a> An <dfn>element-by-element boolean expression</dfn> is a combination of comparison expressions using the boolean operators “or” (‘<samp><span class="samp">|</span></samp>’), “and” (‘<samp><span class="samp">&</span></samp>’), and “not” (‘<samp><span class="samp">!</span></samp>’), along with parentheses to control nesting. The truth of the boolean expression is computed by combining the truth values of the corresponding elements of the component expressions. A value is considered to be false if it is zero, and true otherwise. <p>Element-by-element boolean expressions can be used wherever comparison expressions can be used. They can be used in <code>if</code> and <code>while</code> statements. However, a matrix value used as the condition in an <code>if</code> or <code>while</code> statement is only true if <em>all</em> of its elements are nonzero. <p>Like comparison operations, each element of an element-by-element boolean expression also has a numeric value (1 if true, 0 if false) that comes into play if the result of the boolean expression is stored in a variable, or used in arithmetic. <p>Here are descriptions of the three element-by-element boolean operators. <dl> <dt><var>boolean1</var><code> & </code><var>boolean2</var><dd><a name="index-g_t_0026-639"></a>Elements of the result are true if both corresponding elements of <var>boolean1</var> and <var>boolean2</var> are true. <br><dt><var>boolean1</var><code> | </code><var>boolean2</var><dd><a name="index-g_t_007c-640"></a>Elements of the result are true if either of the corresponding elements of <var>boolean1</var> or <var>boolean2</var> is true. <br><dt><code>! </code><var>boolean</var><dt><code>~ </code><var>boolean</var><dd><a name="index-g_t_007e-641"></a><a name="index-g_t_0021-642"></a>Each element of the result is true if the corresponding element of <var>boolean</var> is false. </dl> <p>These operators work on an element-by-element basis. For example, the expression <pre class="example"> [1, 0; 0, 1] & [1, 0; 2, 3] </pre> <p class="noindent">returns a two by two identity matrix. <p>For the binary operators, broadcasting rules apply. See <a href="Broadcasting.html#Broadcasting">Broadcasting</a>. In particular, if one of the operands is a scalar and the other a matrix, the operator is applied to the scalar and each element of the matrix. <p>For the binary element-by-element boolean operators, both subexpressions <var>boolean1</var> and <var>boolean2</var> are evaluated before computing the result. This can make a difference when the expressions have side effects. For example, in the expression <pre class="example"> a & b++ </pre> <p class="noindent">the value of the variable <var>b</var> is incremented even if the variable <var>a</var> is zero. <p>This behavior is necessary for the boolean operators to work as described for matrix-valued operands. <p><a name="index-g_t_0026-643"></a><!-- and src/data.cc --> <a name="doc_002dand"></a> <div class="defun"> — Built-in Function: <b>and</b> (<var>x, y</var>)<var><a name="index-and-644"></a></var><br> — Built-in Function: <b>and</b> (<var>x1, x2, <small class="dots">...</small></var>)<var><a name="index-and-645"></a></var><br> <blockquote><p>Return the logical AND of <var>x</var> and <var>y</var>. This function is equivalent to <code>x & y</code><!-- /@w -->. If more arguments are given, the logical and is applied cumulatively from left to right: <pre class="example"> (...((x1 & x2) & x3) & ...) </pre> <p>At least one argument is required. <!-- 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_002dor.html#doc_002dor">or</a>, <a href="doc_002dnot.html#doc_002dnot">not</a>, <a href="doc_002dxor.html#doc_002dxor">xor</a>. </p></blockquote></div> <p><a name="index-g_t_007e-646"></a><a name="index-g_t_0021-647"></a><!-- not src/data.cc --> <a name="doc_002dnot"></a> <div class="defun"> — Built-in Function: <b>not</b> (<var>x</var>)<var><a name="index-not-648"></a></var><br> <blockquote><p>Return the logical NOT of <var>x</var>. This function is equivalent to <code>! x</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_002dand.html#doc_002dand">and</a>, <a href="doc_002dor.html#doc_002dor">or</a>, <a href="doc_002dxor.html#doc_002dxor">xor</a>. </p></blockquote></div> <p><a name="index-g_t_007c-649"></a><!-- or src/data.cc --> <a name="doc_002dor"></a> <div class="defun"> — Built-in Function: <b>or</b> (<var>x, y</var>)<var><a name="index-or-650"></a></var><br> — Built-in Function: <b>or</b> (<var>x1, x2, <small class="dots">...</small></var>)<var><a name="index-or-651"></a></var><br> <blockquote><p>Return the logical OR of <var>x</var> and <var>y</var>. This function is equivalent to <code>x | y</code><!-- /@w -->. If more arguments are given, the logical or is applied cumulatively from left to right: <pre class="example"> (...((x1 | x2) | x3) | ...) </pre> <p>At least one argument is required. <!-- 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_002dnot.html#doc_002dnot">not</a>, <a href="doc_002dxor.html#doc_002dxor">xor</a>. </p></blockquote></div> </body></html>