File: Element_002dby_002delement-Boolean-Operators.html

package info (click to toggle)
octave 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 124,192 kB
  • sloc: cpp: 322,665; ansic: 68,088; fortran: 20,980; objc: 8,121; sh: 7,719; yacc: 4,266; lex: 4,123; perl: 1,530; java: 1,366; awk: 1,257; makefile: 424; xml: 147
file content (182 lines) | stat: -rw-r--r-- 8,224 bytes parent folder | download
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Element-by-element Boolean Operators (GNU Octave (version 6.2.0))</title>

<meta name="description" content="Element-by-element Boolean Operators (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Element-by-element Boolean Operators (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Boolean-Expressions.html" rel="up" title="Boolean Expressions">
<link href="Short_002dcircuit-Boolean-Operators.html" rel="next" title="Short-circuit Boolean Operators">
<link href="Boolean-Expressions.html" rel="prev" title="Boolean Expressions">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {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}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<span id="Element_002dby_002delement-Boolean-Operators"></span><div class="header">
<p>
Next: <a href="Short_002dcircuit-Boolean-Operators.html" accesskey="n" rel="next">Short-circuit Boolean Operators</a>, Up: <a href="Boolean-Expressions.html" accesskey="u" rel="up">Boolean Expressions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Element_002dby_002delement-Boolean-Operators-1"></span><h4 class="subsection">8.5.1 Element-by-element Boolean Operators</h4>
<span id="index-element_002dby_002delement-evaluation"></span>

<p>An <em>element-by-element boolean expression</em> is a combination of
comparison expressions using the boolean
operators &ldquo;or&rdquo; (&lsquo;<samp>|</samp>&rsquo;), &ldquo;and&rdquo; (&lsquo;<samp>&amp;</samp>&rsquo;), and &ldquo;not&rdquo; (&lsquo;<samp>!</samp>&rsquo;),
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> &amp; <var>boolean2</var></code></dt>
<dd><span id="index-_0026"></span>
<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><span id="index-_007c"></span>
<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><span id="index-_007e"></span>
<span id="index-_0021"></span>
<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] &amp; [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</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 &amp; 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>
<span id="index-_0026-1"></span>
<span id="XREFand"></span><dl>
<dt id="index-and">: <em><var>z</var> =</em> <strong>and</strong> <em>(<var>x</var>, <var>y</var>)</em></dt>
<dt id="index-and-1">: <em><var>z</var> =</em> <strong>and</strong> <em>(<var>x1</var>, <var>x2</var>, &hellip;)</em></dt>
<dd><p>Return the logical AND of <var>x</var> and <var>y</var>.
</p>
<p>This function is equivalent to the operator syntax
<code><var>x</var>&nbsp;&amp;&nbsp;<var>y</var></code><!-- /@w -->.  If more than two arguments are given, the
logical AND is applied cumulatively from left to right:
</p>
<div class="example">
<pre class="example">(&hellip;((<var>x1</var> &amp; <var>x2</var>) &amp; <var>x3</var>) &amp; &hellip;)
</pre></div>


<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>


<span id="index-_007e-1"></span>
<span id="index-_0021-1"></span>
<span id="XREFnot"></span><dl>
<dt id="index-not">: <em><var>z</var> =</em> <strong>not</strong> <em>(<var>x</var>)</em></dt>
<dd><p>Return the logical NOT of <var>x</var>.
</p>
<p>This function is equivalent to the operator syntax <code>!&nbsp;<var>x</var></code><!-- /@w -->.
</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>


<span id="index-_007c-1"></span>
<span id="XREFor"></span><dl>
<dt id="index-or">: <em><var>z</var> =</em> <strong>or</strong> <em>(<var>x</var>, <var>y</var>)</em></dt>
<dt id="index-or-1">: <em><var>z</var> =</em> <strong>or</strong> <em>(<var>x1</var>, <var>x2</var>, &hellip;)</em></dt>
<dd><p>Return the logical OR of <var>x</var> and <var>y</var>.
</p>
<p>This function is equivalent to the operator syntax
<code><var>x</var>&nbsp;|&nbsp;<var>y</var></code><!-- /@w -->.  If more than two arguments are given, the
logical OR is applied cumulatively from left to right:
</p>
<div class="example">
<pre class="example">(&hellip;((<var>x1</var> | <var>x2</var>) | <var>x3</var>) | &hellip;)
</pre></div>


<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" accesskey="n" rel="next">Short-circuit Boolean Operators</a>, Up: <a href="Boolean-Expressions.html" accesskey="u" rel="up">Boolean Expressions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>