1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Element-by-element Boolean Operators</title>
<meta name="description" content="GNU Octave: Element-by-element Boolean Operators">
<meta name="keywords" content="GNU Octave: Element-by-element Boolean Operators">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Boolean-Expressions.html#Boolean-Expressions" rel="up" title="Boolean Expressions">
<link href="Short_002dcircuit-Boolean-Operators.html#Short_002dcircuit-Boolean-Operators" rel="next" title="Short-circuit Boolean Operators">
<link href="Boolean-Expressions.html#Boolean-Expressions" rel="prev" title="Boolean Expressions">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Element_002dby_002delement-Boolean-Operators"></a>
<div class="header">
<p>
Next: <a href="Short_002dcircuit-Boolean-Operators.html#Short_002dcircuit-Boolean-Operators" accesskey="n" rel="next">Short-circuit Boolean Operators</a>, Up: <a href="Boolean-Expressions.html#Boolean-Expressions" accesskey="u" rel="up">Boolean Expressions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Element_002dby_002delement-Boolean-Operators-1"></a>
<h4 class="subsection">8.5.1 Element-by-element Boolean Operators</h4>
<a name="index-element_002dby_002delement-evaluation"></a>
<p>An <em>element-by-element boolean expression</em> is a combination of
comparison expressions using the boolean
operators “or” (‘<samp>|</samp>’), “and” (‘<samp>&</samp>’), and “not” (‘<samp>!</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>
<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>
<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>
<p>Here are descriptions of the three element-by-element boolean operators.
</p>
<dl compact="compact">
<dt><code><var>boolean1</var> & <var>boolean2</var></code></dt>
<dd><a name="index-_0026"></a>
<p>Elements of the result are true if both corresponding elements of
<var>boolean1</var> and <var>boolean2</var> are true.
</p>
</dd>
<dt><code><var>boolean1</var> | <var>boolean2</var></code></dt>
<dd><a name="index-_007c"></a>
<p>Elements of the result are true if either of the corresponding elements
of <var>boolean1</var> or <var>boolean2</var> is true.
</p>
</dd>
<dt><code>! <var>boolean</var></code></dt>
<dt><code>~ <var>boolean</var></code></dt>
<dd><a name="index-_007e"></a>
<a name="index-_0021"></a>
<p>Each element of the result is true if the corresponding element of
<var>boolean</var> is false.
</p></dd>
</dl>
<p>These operators work on an element-by-element basis. For example, the
expression
</p>
<div class="example">
<pre class="example">[1, 0; 0, 1] & [1, 0; 2, 3]
</pre></div>
<p>returns a two by two identity matrix.
</p>
<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>
<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
</p>
<div class="example">
<pre class="example">a & b++
</pre></div>
<p>the value of the variable <var>b</var> is incremented even if the variable
<var>a</var> is zero.
</p>
<p>This behavior is necessary for the boolean operators to work as
described for matrix-valued operands.
</p>
<a name="index-_0026-1"></a>
<a name="XREFand"></a><dl>
<dt><a name="index-and"></a>Built-in Function: <em></em> <strong>and</strong> <em>(<var>x</var>, <var>y</var>)</em></dt>
<dt><a name="index-and-1"></a>Built-in Function: <em></em> <strong>and</strong> <em>(<var>x1</var>, <var>x2</var>, …)</em></dt>
<dd><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:
</p>
<div class="example">
<pre class="example">(…((x1 & x2) & x3) & …)
</pre></div>
<p>At least one argument is required.
</p>
<p><strong>See also:</strong> <a href="#XREFor">or</a>, <a href="#XREFnot">not</a>, <a href="Finding-Elements-and-Checking-Conditions.html#XREFxor">xor</a>.
</p></dd></dl>
<a name="index-_007e-1"></a>
<a name="index-_0021-1"></a>
<a name="XREFnot"></a><dl>
<dt><a name="index-not"></a>Built-in Function: <em></em> <strong>not</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return the logical NOT of <var>x</var>. This function is equivalent to
<code>! x</code>.
</p>
<p><strong>See also:</strong> <a href="#XREFand">and</a>, <a href="#XREFor">or</a>, <a href="Finding-Elements-and-Checking-Conditions.html#XREFxor">xor</a>.
</p></dd></dl>
<a name="index-_007c-1"></a>
<a name="XREFor"></a><dl>
<dt><a name="index-or"></a>Built-in Function: <em></em> <strong>or</strong> <em>(<var>x</var>, <var>y</var>)</em></dt>
<dt><a name="index-or-1"></a>Built-in Function: <em></em> <strong>or</strong> <em>(<var>x1</var>, <var>x2</var>, …)</em></dt>
<dd><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:
</p>
<div class="example">
<pre class="example">(…((x1 | x2) | x3) | …)
</pre></div>
<p>At least one argument is required.
</p>
<p><strong>See also:</strong> <a href="#XREFand">and</a>, <a href="#XREFnot">not</a>, <a href="Finding-Elements-and-Checking-Conditions.html#XREFxor">xor</a>.
</p></dd></dl>
<hr>
<div class="header">
<p>
Next: <a href="Short_002dcircuit-Boolean-Operators.html#Short_002dcircuit-Boolean-Operators" accesskey="n" rel="next">Short-circuit Boolean Operators</a>, Up: <a href="Boolean-Expressions.html#Boolean-Expressions" accesskey="u" rel="up">Boolean Expressions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|