/usr/share/doc/octave-htmldoc/interpreter/Assignment-Ops.html is in octave-htmldoc 3.8.2-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | <!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: Assignment Ops</title>
<meta name="description" content="GNU Octave: Assignment Ops">
<meta name="keywords" content="GNU Octave: Assignment Ops">
<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="Expressions.html#Expressions" rel="up" title="Expressions">
<link href="Increment-Ops.html#Increment-Ops" rel="next" title="Increment Ops">
<link href="Short_002dcircuit-Boolean-Operators.html#Short_002dcircuit-Boolean-Operators" rel="prev" title="Short-circuit Boolean Operators">
<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="Assignment-Ops"></a>
<div class="header">
<p>
Next: <a href="Increment-Ops.html#Increment-Ops" accesskey="n" rel="next">Increment Ops</a>, Previous: <a href="Boolean-Expressions.html#Boolean-Expressions" accesskey="p" rel="prev">Boolean Expressions</a>, Up: <a href="Expressions.html#Expressions" accesskey="u" rel="up">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="Assignment-Expressions"></a>
<h3 class="section">8.6 Assignment Expressions</h3>
<a name="index-assignment-expressions"></a>
<a name="index-assignment-operators"></a>
<a name="index-operators_002c-assignment"></a>
<a name="index-expressions_002c-assignment"></a>
<a name="index-_003d"></a>
<p>An <em>assignment</em> is an expression that stores a new value into a
variable. For example, the following expression assigns the value 1 to
the variable <code>z</code>:
</p>
<div class="example">
<pre class="example">z = 1
</pre></div>
<p>After this expression is executed, the variable <code>z</code> has the value 1.
Whatever old value <code>z</code> had before the assignment is forgotten.
The ‘<samp>=</samp>’ sign is called an <em>assignment operator</em>.
</p>
<p>Assignments can store string values also. For example, the following
expression would store the value <code>"this food is good"</code> in the
variable <code>message</code>:
</p>
<div class="example">
<pre class="example">thing = "food"
predicate = "good"
message = [ "this " , thing , " is " , predicate ]
</pre></div>
<p>(This also illustrates concatenation of strings.)
</p>
<a name="index-side-effect"></a>
<p>Most operators (addition, concatenation, and so on) have no effect
except to compute a value. If you ignore the value, you might as well
not use the operator. An assignment operator is different. It does
produce a value, but even if you ignore the value, the assignment still
makes itself felt through the alteration of the variable. We call this
a <em>side effect</em>.
</p>
<a name="index-lvalue"></a>
<p>The left-hand operand of an assignment need not be a variable
(see <a href="Variables.html#Variables">Variables</a>). It can also be an element of a matrix
(see <a href="Index-Expressions.html#Index-Expressions">Index Expressions</a>) or a list of return values
(see <a href="Calling-Functions.html#Calling-Functions">Calling Functions</a>). These are all called <em>lvalues</em>, which
means they can appear on the left-hand side of an assignment operator.
The right-hand operand may be any expression. It produces the new value
which the assignment stores in the specified variable, matrix element,
or list of return values.
</p>
<p>It is important to note that variables do <em>not</em> have permanent types.
The type of a variable is simply the type of whatever value it happens
to hold at the moment. In the following program fragment, the variable
<code>foo</code> has a numeric value at first, and a string value later on:
</p>
<div class="example">
<pre class="example">octave:13> foo = 1
foo = 1
octave:13> foo = "bar"
foo = bar
</pre></div>
<p>When the second assignment gives <code>foo</code> a string value, the fact that
it previously had a numeric value is forgotten.
</p>
<p>Assignment of a scalar to an indexed matrix sets all of the elements
that are referenced by the indices to the scalar value. For example, if
<code>a</code> is a matrix with at least two columns,
</p>
<div class="example">
<pre class="example">a(:, 2) = 5
</pre></div>
<p>sets all the elements in the second column of <code>a</code> to 5.
</p>
<p>Assigning an empty matrix ‘<samp>[]</samp>’ works in most cases to allow you to
delete rows or columns of matrices and vectors. See <a href="Empty-Matrices.html#Empty-Matrices">Empty Matrices</a>.
For example, given a 4 by 5 matrix <var>A</var>, the assignment
</p>
<div class="example">
<pre class="example">A (3, :) = []
</pre></div>
<p>deletes the third row of <var>A</var>, and the assignment
</p>
<div class="example">
<pre class="example">A (:, 1:2:5) = []
</pre></div>
<p>deletes the first, third, and fifth columns.
</p>
<p>An assignment is an expression, so it has a value. Thus, <code>z = 1</code>
as an expression has the value 1. One consequence of this is that you
can write multiple assignments together:
</p>
<div class="example">
<pre class="example">x = y = z = 0
</pre></div>
<p>stores the value 0 in all three variables. It does this because the
value of <code>z = 0</code>, which is 0, is stored into <code>y</code>, and then
the value of <code>y = z = 0</code>, which is 0, is stored into <code>x</code>.
</p>
<p>This is also true of assignments to lists of values, so the following is
a valid expression
</p>
<div class="example">
<pre class="example">[a, b, c] = [u, s, v] = svd (a)
</pre></div>
<p>that is exactly equivalent to
</p>
<div class="example">
<pre class="example">[u, s, v] = svd (a)
a = u
b = s
c = v
</pre></div>
<p>In expressions like this, the number of values in each part of the
expression need not match. For example, the expression
</p>
<div class="example">
<pre class="example">[a, b] = [u, s, v] = svd (a)
</pre></div>
<p>is equivalent to
</p>
<div class="example">
<pre class="example">[u, s, v] = svd (a)
a = u
b = s
</pre></div>
<p>The number of values on the left side of the expression can, however,
not exceed the number of values on the right side. For example, the
following will produce an error.
</p>
<div class="example">
<pre class="example">[a, b, c, d] = [u, s, v] = svd (a);
-| error: element number 4 undefined in return list
</pre></div>
<p>The symbol <code>~</code> may be used as a placeholder in the list of lvalues,
indicating that the corresponding return value should be ignored and not stored
anywhere:
</p>
<div class="example">
<pre class="example">[~, s, v] = svd (a);
</pre></div>
<p>This is cleaner and more memory efficient than using a dummy variable.
The <code>nargout</code> value for the right-hand side expression is not affected.
If the assignment is used as an expression, the return value is a
comma-separated list with the ignored values dropped.
</p>
<a name="index-_002b_003d"></a>
<p>A very common programming pattern is to increment an existing variable
with a given value, like this
</p>
<div class="example">
<pre class="example">a = a + 2;
</pre></div>
<p>This can be written in a clearer and more condensed form using the
<code>+=</code> operator
</p>
<div class="example">
<pre class="example">a += 2;
</pre></div>
<a name="index-_002d_003d"></a>
<a name="index-_002a_003d"></a>
<a name="index-_002f_003d"></a>
<p>Similar operators also exist for subtraction (<code>-=</code>),
multiplication (<code>*=</code>), and division (<code>/=</code>). An expression
of the form
</p>
<div class="example">
<pre class="example"><var>expr1</var> <var>op</var>= <var>expr2</var>
</pre></div>
<p>is evaluated as
</p>
<div class="example">
<pre class="example"><var>expr1</var> = (<var>expr1</var>) <var>op</var> (<var>expr2</var>)
</pre></div>
<p>where <var>op</var> can be either <code>+</code>, <code>-</code>, <code>*</code>, or <code>/</code>,
as long as <var>expr2</var> is a simple expression with no side effects. If
<var>expr2</var> also contains an assignment operator, then this expression
is evaluated as
</p>
<div class="example">
<pre class="example"><var>temp</var> = <var>expr2</var>
<var>expr1</var> = (<var>expr1</var>) <var>op</var> <var>temp</var>
</pre></div>
<p>where <var>temp</var> is a placeholder temporary value storing the computed
result of evaluating <var>expr2</var>. So, the expression
</p>
<div class="example">
<pre class="example">a *= b+1
</pre></div>
<p>is evaluated as
</p>
<div class="example">
<pre class="example">a = a * (b+1)
</pre></div>
<p>and <em>not</em>
</p>
<div class="example">
<pre class="example">a = a * b + 1
</pre></div>
<p>You can use an assignment anywhere an expression is called for. For
example, it is valid to write <code>x != (y = 1)</code> to set <code>y</code> to 1
and then test whether <code>x</code> equals 1. But this style tends to make
programs hard to read. Except in a one-shot program, you should rewrite
it to get rid of such nesting of assignments. This is never very hard.
</p>
<a name="index-increment-operator"></a>
<a name="index-decrement-operator"></a>
<a name="index-operators_002c-increment"></a>
<a name="index-operators_002c-decrement"></a>
<hr>
<div class="header">
<p>
Next: <a href="Increment-Ops.html#Increment-Ops" accesskey="n" rel="next">Increment Ops</a>, Previous: <a href="Boolean-Expressions.html#Boolean-Expressions" accesskey="p" rel="prev">Boolean Expressions</a>, Up: <a href="Expressions.html#Expressions" accesskey="u" rel="up">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>
|