Mercurial > hg > octave-lyh
annotate doc/interpreter/expr.txi @ 17483:bddb9688e41c
feather.m: Overhaul function for Matlab visual compatibility.
* scripts/plot/feather.m: Reduce length of arrowhead size to 0.20 of arrow
shaft. Change arrow head angle with shaft from 45 to 30 degrees. Use variable
'h' instead of 'retval' to match documentation. Redo input validation.
Add %!error input validation tests.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 24 Sep 2013 13:17:04 -0700 |
parents | bc924baa2c4e |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14119
diff
changeset
|
1 @c Copyright (C) 1996-2012 John W. Eaton |
7018 | 2 @c |
3 @c This file is part of Octave. | |
4 @c | |
5 @c Octave is free software; you can redistribute it and/or modify it | |
6 @c under the terms of the GNU General Public License as published by the | |
7 @c Free Software Foundation; either version 3 of the License, or (at | |
8 @c your option) any later version. | |
9 @c | |
10 @c Octave is distributed in the hope that it will be useful, but WITHOUT | |
11 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 @c for more details. | |
14 @c | |
15 @c You should have received a copy of the GNU General Public License | |
16 @c along with Octave; see the file COPYING. If not, see | |
17 @c <http://www.gnu.org/licenses/>. | |
3294 | 18 |
4167 | 19 @node Expressions |
3294 | 20 @chapter Expressions |
21 @cindex expressions | |
22 | |
23 Expressions are the basic building block of statements in Octave. An | |
24 expression evaluates to a value, which you can print, test, store in a | |
25 variable, pass to a function, or assign a new value to a variable with | |
26 an assignment operator. | |
27 | |
28 An expression can serve as a statement on its own. Most other kinds of | |
29 statements contain one or more expressions which specify data to be | |
30 operated on. As in other languages, expressions in Octave include | |
31 variables, array references, constants, and function calls, as well as | |
32 combinations of these with various operators. | |
33 | |
34 @menu | |
17160
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
35 * Index Expressions:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
36 * Calling Functions:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
37 * Arithmetic Ops:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
38 * Comparison Ops:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
39 * Boolean Expressions:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
40 * Assignment Ops:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
41 * Increment Ops:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
42 * Operator Precedence:: |
3294 | 43 @end menu |
44 | |
4167 | 45 @node Index Expressions |
3294 | 46 @section Index Expressions |
47 | |
48 @opindex ( | |
49 @opindex ) | |
11403
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
50 @opindex : |
3294 | 51 |
52 An @dfn{index expression} allows you to reference or extract selected | |
53 elements of a matrix or vector. | |
54 | |
55 Indices may be scalars, vectors, ranges, or the special operator | |
56 @samp{:}, which may be used to select entire rows or columns. | |
57 | |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
58 Vectors are indexed using a single index expression. Matrices (2-D) |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
59 and higher multi-dimensional arrays are indexed using either one index |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
60 or @math{N} indices where @math{N} is the dimension of the array. |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
61 When using a single index expression to index 2-D or higher data the |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
62 elements of the array are taken in column-first order (like Fortran). |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
63 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
64 The output from indexing assumes the dimensions of the index |
14038
b0cdd60db5e5
doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14033
diff
changeset
|
65 expression. For example: |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
66 |
5679 | 67 @example |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9159
diff
changeset
|
68 @group |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
69 a(2) # result is a scalar |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
70 a(1:2) # result is a row vector |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
71 a([1; 2]) # result is a column vector |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9159
diff
changeset
|
72 @end group |
5679 | 73 @end example |
74 | |
75 As a special case, when a colon is used as a single index, the output | |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
76 is a column vector containing all the elements of the vector or |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
77 matrix. For example: |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
78 |
5679 | 79 @example |
14038
b0cdd60db5e5
doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14033
diff
changeset
|
80 @group |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
81 a(:) # result is a column vector |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
82 a(:)' # result is a row vector |
14038
b0cdd60db5e5
doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14033
diff
changeset
|
83 @end group |
5679 | 84 @end example |
85 | |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
86 The above two code idioms are often used in place of @code{reshape} |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
87 when a simple vector, rather than an arbitrarily sized array, is |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
88 needed. |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
89 |
3294 | 90 Given the matrix |
91 | |
92 @example | |
93 a = [1, 2; 3, 4] | |
94 @end example | |
95 | |
96 @noindent | |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
97 all of the following expressions are equivalent and select the first |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
98 row of the matrix. |
3294 | 99 |
100 @example | |
101 @group | |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
102 a(1, [1, 2]) # row 1, columns 1 and 2 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
103 a(1, 1:2) # row 1, columns in range 1-2 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
104 a(1, :) # row 1, all columns |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
105 @end group |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
106 @end example |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
107 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
108 @cindex @code{end}, indexing |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
109 @cindex :end |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
110 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
111 In index expressions the keyword @code{end} automatically refers to |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
112 the last entry for a particular dimension. This magic index can also |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
113 be used in ranges and typically eliminates the needs to call |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
114 @code{size} or @code{length} to gather array bounds before indexing. |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
115 For example: |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
116 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
117 @example |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
118 @group |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
119 a = [1, 2, 3, 4]; |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
120 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
121 a(1:end/2) # first half of a => [1, 2] |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
122 a(end + 1) = 5; # append element |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
123 a(end) = []; # delete element |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
124 a(1:2:end) # odd elements of a => [1, 3] |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
125 a(2:2:end) # even elements of a => [2, 4] |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
126 a(end:-1:1) # reversal of a => [4, 3, 2 , 1] |
3294 | 127 @end group |
128 @end example | |
129 | |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
130 @menu |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
131 * Advanced Indexing:: |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
132 @end menu |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
133 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
134 @node Advanced Indexing |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
135 @subsection Advanced Indexing |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
136 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
137 An array with @samp{n} dimensions can be indexed using @samp{m} |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
138 indices. More generally, the set of index tuples determining the |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
139 result is formed by the Cartesian product of the index vectors (or |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
140 ranges or scalars). |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
141 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
142 For the ordinary and most common case, @w{@code{m == n}}, and each |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
143 index corresponds to its respective dimension. If @w{@code{m < n}} |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
144 and every index is less than the size of the array in the |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
145 @math{i^{th}} dimension, @code{m(i) < n(i)}, then the index expression |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
146 is padded with trailing singleton dimensions (@code{[ones (m-n, 1)]}). |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
147 If @w{@code{m < n}} but one of the indices @code{m(i)} is outside the |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
148 size of the current array, then the last @w{@code{n-m+1}} dimensions |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
149 are folded into a single dimension with an extent equal to the product |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
150 of extents of the original dimensions. This is easiest to understand |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
151 with an example. |
3294 | 152 |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
153 @example |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
154 a = reshape (1:8, 2, 2, 2) # Create 3-D array |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
155 a = |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
156 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
157 ans(:,:,1) = |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
158 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
159 1 3 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
160 2 4 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
161 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
162 ans(:,:,2) = |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
163 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
164 5 7 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
165 6 8 |
9159 | 166 |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
167 a(2,1,2); # Case (m == n): ans = 6 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
168 a(2,1); # Case (m < n), idx within array: |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
169 # equivalent to a(2,1,1), ans = 2 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
170 a(2,4); # Case (m < n), idx outside array: |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
171 # Dimension 2 & 3 folded into new dimension of size 2x2 = 4 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
172 # Select 2nd row, 4th element of [2, 4, 6, 8], ans = 8 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
173 @end example |
3294 | 174 |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
175 One advanced use of indexing is to create arrays filled with a single |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
176 value. This can be done by using an index of ones on a scalar value. |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
177 The result is an object with the dimensions of the index expression |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
178 and every element equal to the original scalar. For example, the |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
179 following statements |
3294 | 180 |
181 @example | |
182 @group | |
183 a = 13; | |
12546
39ca02387a32
Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents:
12544
diff
changeset
|
184 a(ones (1, 4)) |
3294 | 185 @end group |
186 @end example | |
187 | |
188 @noindent | |
189 produce a vector whose four elements are all equal to 13. | |
190 | |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
191 Similarly, by indexing a scalar with two vectors of ones it is |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
192 possible to create a matrix. The following statements |
3294 | 193 |
194 @example | |
195 @group | |
196 a = 13; | |
12546
39ca02387a32
Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents:
12544
diff
changeset
|
197 a(ones (1, 2), ones (1, 3)) |
3294 | 198 @end group |
199 @end example | |
200 | |
201 @noindent | |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
202 create a 2x3 matrix with all elements equal to 13. |
3294 | 203 |
9159 | 204 The last example could also be written as |
205 | |
206 @example | |
207 @group | |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
208 13(ones (2, 3)) |
9159 | 209 @end group |
210 @end example | |
211 | |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
212 It is more efficient to use indexing rather than the code construction |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
213 @code{scalar * ones (N, M, @dots{})} because it avoids the unnecessary |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
214 multiplication operation. Moreover, multiplication may not be |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
215 defined for the object to be replicated whereas indexing an array is |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
216 always defined. The following code shows how to create a 2x3 cell |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
217 array from a base unit which is not itself a scalar. |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
218 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
219 @example |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
220 @group |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
221 @{"Hello"@}(ones (2, 3)) |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
222 @end group |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
223 @end example |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
224 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
225 It should be, noted that @code{ones (1, n)} (a row vector of ones) |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
226 results in a range (with zero increment). A range is stored |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
227 internally as a starting value, increment, end value, and total number |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
228 of values; hence, it is more efficient for storage than a vector or |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
229 matrix of ones whenever the number of elements is greater than 4. In |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
230 particular, when @samp{r} is a row vector, the expressions |
9159 | 231 |
232 @example | |
233 r(ones (1, n), :) | |
234 @end example | |
235 | |
236 @example | |
237 r(ones (n, 1), :) | |
238 @end example | |
239 | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
240 @noindent |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
241 will produce identical results, but the first one will be |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
242 significantly faster, at least for @samp{r} and @samp{n} large enough. |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
243 In the first case the index is held in compressed form as a range |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
244 which allows Octave to choose a more efficient algorithm to handle the |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
245 expression. |
9159 | 246 |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
247 A general recommendation, for a user unaware of these subtleties, is |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
248 to use the function @code{repmat} for replicating smaller arrays into |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
249 bigger ones. |
3294 | 250 |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
251 A second use of indexing is to speed up code. Indexing is a fast |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
252 operation and judicious use of it can reduce the requirement for |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
253 looping over individual array elements which is a slow operation. |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
254 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
255 Consider the following example which creates a 10-element row vector |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
256 @math{a} containing the values |
6642 | 257 @tex |
258 $a_i = \sqrt{i}$. | |
259 @end tex | |
260 @ifnottex | |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14171
diff
changeset
|
261 a(i) = sqrt (i). |
6642 | 262 @end ifnottex |
263 | |
264 @example | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9159
diff
changeset
|
265 @group |
6642 | 266 for i = 1:10 |
267 a(i) = sqrt (i); | |
268 endfor | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9159
diff
changeset
|
269 @end group |
6642 | 270 @end example |
271 | |
272 @noindent | |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
273 It is quite inefficient to create a vector using a loop like this. In |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
274 this case, it would have been much more efficient to use the |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
275 expression |
3294 | 276 |
277 @example | |
278 a = sqrt (1:10); | |
279 @end example | |
280 | |
281 @noindent | |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
282 which avoids the loop entirely. |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
283 |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
284 In cases where a loop cannot be avoided, or a number of values must be |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
285 combined to form a larger matrix, it is generally faster to set the |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
286 size of the matrix first (pre-allocate storage), and then insert |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
287 elements using indexing commands. For example, given a matrix |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
288 @code{a}, |
3294 | 289 |
290 @example | |
291 @group | |
292 [nr, nc] = size (a); | |
293 x = zeros (nr, n * nc); | |
294 for i = 1:n | |
3602 | 295 x(:,(i-1)*nc+1:i*nc) = a; |
3294 | 296 endfor |
297 @end group | |
298 @end example | |
299 | |
300 @noindent | |
301 is considerably faster than | |
302 | |
303 @example | |
304 @group | |
305 x = a; | |
306 for i = 1:n-1 | |
307 x = [x, a]; | |
308 endfor | |
309 @end group | |
310 @end example | |
311 | |
312 @noindent | |
14033
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
313 because Octave does not have to repeatedly resize the intermediate |
dfbf6a49847c
doc: Update documentation on indexing expressions
Rik <octave@nomad.inbox5.com>
parents:
13780
diff
changeset
|
314 result. |
3294 | 315 |
6549 | 316 @DOCSTRING(sub2ind) |
317 | |
318 @DOCSTRING(ind2sub) | |
319 | |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
11403
diff
changeset
|
320 @DOCSTRING(isindex) |
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
11403
diff
changeset
|
321 |
12544
3ba6e702e6fd
Add allow_noninteger_range_as_index to documentation.
Rik <octave@nomad.inbox5.com>
parents:
12543
diff
changeset
|
322 @DOCSTRING(allow_noninteger_range_as_index) |
3ba6e702e6fd
Add allow_noninteger_range_as_index to documentation.
Rik <octave@nomad.inbox5.com>
parents:
12543
diff
changeset
|
323 |
4167 | 324 @node Calling Functions |
3294 | 325 @section Calling Functions |
326 | |
327 A @dfn{function} is a name for a particular calculation. Because it has | |
328 a name, you can ask for it by name at any point in the program. For | |
329 example, the function @code{sqrt} computes the square root of a number. | |
330 | |
331 A fixed set of functions are @dfn{built-in}, which means they are | |
332 available in every Octave program. The @code{sqrt} function is one of | |
333 these. In addition, you can define your own functions. | |
334 @xref{Functions and Scripts}, for information about how to do this. | |
335 | |
336 @cindex arguments in function call | |
337 The way to use a function is with a @dfn{function call} expression, | |
338 which consists of the function name followed by a list of | |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
339 @dfn{arguments} in parentheses. The arguments are expressions which give |
3294 | 340 the raw materials for the calculation that the function will do. When |
341 there is more than one argument, they are separated by commas. If there | |
342 are no arguments, you can omit the parentheses, but it is a good idea to | |
343 include them anyway, to clearly indicate that a function call was | |
344 intended. Here are some examples: | |
345 | |
346 @example | |
347 @group | |
348 sqrt (x^2 + y^2) # @r{One argument} | |
349 ones (n, m) # @r{Two arguments} | |
350 rand () # @r{No arguments} | |
351 @end group | |
352 @end example | |
353 | |
354 Each function expects a particular number of arguments. For example, the | |
355 @code{sqrt} function must be called with a single argument, the number | |
356 to take the square root of: | |
357 | |
358 @example | |
359 sqrt (@var{argument}) | |
360 @end example | |
361 | |
362 Some of the built-in functions take a variable number of arguments, | |
363 depending on the particular usage, and their behavior is different | |
364 depending on the number of arguments supplied. | |
365 | |
366 Like every other expression, the function call has a value, which is | |
367 computed by the function based on the arguments you give it. In this | |
368 example, the value of @code{sqrt (@var{argument})} is the square root of | |
369 the argument. A function can also have side effects, such as assigning | |
370 the values of certain variables or doing input or output operations. | |
371 | |
372 Unlike most languages, functions in Octave may return multiple values. | |
373 For example, the following statement | |
374 | |
375 @example | |
376 [u, s, v] = svd (a) | |
377 @end example | |
378 | |
379 @noindent | |
380 computes the singular value decomposition of the matrix @code{a} and | |
381 assigns the three result matrices to @code{u}, @code{s}, and @code{v}. | |
382 | |
383 The left side of a multiple assignment expression is itself a list of | |
384 expressions, and is allowed to be a list of variable names or index | |
385 expressions. See also @ref{Index Expressions}, and @ref{Assignment Ops}. | |
386 | |
387 @menu | |
17160
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
388 * Call by Value:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
389 * Recursion:: |
3294 | 390 @end menu |
391 | |
4167 | 392 @node Call by Value |
3294 | 393 @subsection Call by Value |
394 | |
395 In Octave, unlike Fortran, function arguments are passed by value, which | |
396 means that each argument in a function call is evaluated and assigned to | |
397 a temporary location in memory before being passed to the function. | |
398 There is currently no way to specify that a function parameter should be | |
399 passed by reference instead of by value. This means that it is | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8235
diff
changeset
|
400 impossible to directly alter the value of a function parameter in the |
3294 | 401 calling function. It can only change the local copy within the function |
402 body. For example, the function | |
403 | |
404 @example | |
405 @group | |
406 function f (x, n) | |
407 while (n-- > 0) | |
408 disp (x); | |
409 endwhile | |
410 endfunction | |
411 @end group | |
412 @end example | |
413 | |
414 @noindent | |
415 displays the value of the first argument @var{n} times. In this | |
416 function, the variable @var{n} is used as a temporary variable without | |
417 having to worry that its value might also change in the calling | |
418 function. Call by value is also useful because it is always possible to | |
419 pass constants for any function parameter without first having to | |
420 determine that the function will not attempt to modify the parameter. | |
421 | |
422 The caller may use a variable as the expression for the argument, but | |
423 the called function does not know this: it only knows what value the | |
424 argument had. For example, given a function called as | |
425 | |
426 @example | |
427 @group | |
428 foo = "bar"; | |
429 fcn (foo) | |
430 @end group | |
431 @end example | |
432 | |
433 @noindent | |
434 you should not think of the argument as being ``the variable | |
435 @code{foo}.'' Instead, think of the argument as the string value, | |
17289
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17178
diff
changeset
|
436 @qcode{"bar"}. |
3294 | 437 |
438 Even though Octave uses pass-by-value semantics for function arguments, | |
439 values are not copied unnecessarily. For example, | |
440 | |
441 @example | |
442 @group | |
443 x = rand (1000); | |
444 f (x); | |
445 @end group | |
446 @end example | |
447 | |
448 @noindent | |
449 does not actually force two 1000 by 1000 element matrices to exist | |
450 @emph{unless} the function @code{f} modifies the value of its | |
451 argument. Then Octave must create a copy to avoid changing the | |
452 value outside the scope of the function @code{f}, or attempting (and | |
453 probably failing!) to modify the value of a constant or the value of a | |
454 temporary result. | |
455 | |
4167 | 456 @node Recursion |
3294 | 457 @subsection Recursion |
458 @cindex factorial function | |
459 | |
6939 | 460 With some restrictions@footnote{Some of Octave's functions are |
3294 | 461 implemented in terms of functions that cannot be called recursively. |
462 For example, the ODE solver @code{lsode} is ultimately implemented in a | |
463 Fortran subroutine that cannot be called recursively, so @code{lsode} | |
464 should not be called either directly or indirectly from within the | |
465 user-supplied function that @code{lsode} requires. Doing so will result | |
6642 | 466 in an error.}, recursive function calls are allowed. A |
3294 | 467 @dfn{recursive function} is one which calls itself, either directly or |
468 indirectly. For example, here is an inefficient@footnote{It would be | |
469 much better to use @code{prod (1:n)}, or @code{gamma (n+1)} instead, | |
470 after first checking to ensure that the value @code{n} is actually a | |
471 positive integer.} way to compute the factorial of a given integer: | |
472 | |
473 @example | |
474 @group | |
475 function retval = fact (n) | |
476 if (n > 0) | |
477 retval = n * fact (n-1); | |
478 else | |
479 retval = 1; | |
480 endif | |
481 endfunction | |
482 @end group | |
483 @end example | |
484 | |
485 This function is recursive because it calls itself directly. It | |
486 eventually terminates because each time it calls itself, it uses an | |
487 argument that is one less than was used for the previous call. Once the | |
488 argument is no longer greater than zero, it does not call itself, and | |
489 the recursion ends. | |
490 | |
491 The built-in variable @code{max_recursion_depth} specifies a limit to | |
492 the recursion depth and prevents Octave from recursing infinitely. | |
493 | |
3371 | 494 @DOCSTRING(max_recursion_depth) |
3294 | 495 |
4167 | 496 @node Arithmetic Ops |
3294 | 497 @section Arithmetic Operators |
498 @cindex arithmetic operators | |
499 @cindex operators, arithmetic | |
500 @cindex addition | |
501 @cindex subtraction | |
502 @cindex multiplication | |
503 @cindex matrix multiplication | |
504 @cindex division | |
505 @cindex quotient | |
506 @cindex negation | |
507 @cindex unary minus | |
508 @cindex exponentiation | |
509 @cindex transpose | |
510 @cindex Hermitian operator | |
511 @cindex transpose, complex-conjugate | |
512 @cindex complex-conjugate transpose | |
513 | |
514 The following arithmetic operators are available, and work on scalars | |
14119
94e2a76f1e5a
doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14116
diff
changeset
|
515 and matrices. The element-by-element operators and functions broadcast |
14116
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14038
diff
changeset
|
516 (@pxref{Broadcasting}). |
3294 | 517 |
13780
990762e784fe
Fix confusing interaction between @code macro and transpose operator (') in documentation (Bug #34661).
Rik <octave@nomad.inbox5.com>
parents:
12627
diff
changeset
|
518 @table @asis |
3294 | 519 @item @var{x} + @var{y} |
520 @opindex + | |
521 Addition. If both operands are matrices, the number of rows and columns | |
14149
f1ff06a1d73a
doc: Mention broadcasting in more relevant places.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
522 must both agree, or they must be broadcastable to the same shape. |
3294 | 523 |
524 @item @var{x} .+ @var{y} | |
525 @opindex .+ | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
526 Element-by-element addition. This operator is equivalent to @code{+}. |
3294 | 527 |
528 @item @var{x} - @var{y} | |
529 @opindex - | |
530 Subtraction. If both operands are matrices, the number of rows and | |
14149
f1ff06a1d73a
doc: Mention broadcasting in more relevant places.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
531 columns of both must agree, or they must be broadcastable to the same |
f1ff06a1d73a
doc: Mention broadcasting in more relevant places.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
532 shape. |
3294 | 533 |
534 @item @var{x} .- @var{y} | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
535 Element-by-element subtraction. This operator is equivalent to @code{-}. |
3294 | 536 |
537 @item @var{x} * @var{y} | |
538 @opindex * | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
539 Matrix multiplication. The number of columns of @var{x} must agree with |
14149
f1ff06a1d73a
doc: Mention broadcasting in more relevant places.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
540 the number of rows of @var{y}, or they must be broadcastable to the same |
f1ff06a1d73a
doc: Mention broadcasting in more relevant places.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
541 shape. |
3294 | 542 |
543 @item @var{x} .* @var{y} | |
544 @opindex .* | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
545 Element-by-element multiplication. If both operands are matrices, the |
14149
f1ff06a1d73a
doc: Mention broadcasting in more relevant places.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
546 number of rows and columns must both agree, or they must be |
f1ff06a1d73a
doc: Mention broadcasting in more relevant places.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
547 broadcastable to the same shape. |
3294 | 548 |
549 @item @var{x} / @var{y} | |
550 @opindex / | |
551 Right division. This is conceptually equivalent to the expression | |
552 | |
553 @example | |
554 (inverse (y') * x')' | |
555 @end example | |
556 | |
557 @noindent | |
558 but it is computed without forming the inverse of @var{y'}. | |
559 | |
560 If the system is not square, or if the coefficient matrix is singular, | |
561 a minimum norm solution is computed. | |
562 | |
563 @item @var{x} ./ @var{y} | |
564 @opindex ./ | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
565 Element-by-element right division. |
3294 | 566 |
567 @item @var{x} \ @var{y} | |
568 @opindex \ | |
569 Left division. This is conceptually equivalent to the expression | |
570 | |
571 @example | |
572 inverse (x) * y | |
573 @end example | |
574 | |
575 @noindent | |
576 but it is computed without forming the inverse of @var{x}. | |
577 | |
578 If the system is not square, or if the coefficient matrix is singular, | |
579 a minimum norm solution is computed. | |
580 | |
581 @item @var{x} .\ @var{y} | |
582 @opindex .\ | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
583 Element-by-element left division. Each element of @var{y} is divided |
3294 | 584 by each corresponding element of @var{x}. |
585 | |
17178
d6499c14021c
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
17160
diff
changeset
|
586 @item @var{x} ^ @var{y} |
3294 | 587 @itemx @var{x} ** @var{y} |
588 @opindex ** | |
589 @opindex ^ | |
590 Power operator. If @var{x} and @var{y} are both scalars, this operator | |
591 returns @var{x} raised to the power @var{y}. If @var{x} is a scalar and | |
592 @var{y} is a square matrix, the result is computed using an eigenvalue | |
7001 | 593 expansion. If @var{x} is a square matrix, the result is computed by |
3294 | 594 repeated multiplication if @var{y} is an integer, and by an eigenvalue |
595 expansion if @var{y} is not an integer. An error results if both | |
596 @var{x} and @var{y} are matrices. | |
597 | |
598 The implementation of this operator needs to be improved. | |
599 | |
17178
d6499c14021c
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
17160
diff
changeset
|
600 @item @var{x} .^ @var{y} |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
601 @itemx @var{x} .** @var{y} |
3294 | 602 @opindex .** |
603 @opindex .^ | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
604 Element-by-element power operator. If both operands are matrices, the |
14149
f1ff06a1d73a
doc: Mention broadcasting in more relevant places.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
605 number of rows and columns must both agree, or they must be |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
606 broadcastable to the same shape. If several complex results are |
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
607 possible, the one with smallest non-negative argument (angle) is taken. |
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
608 This rule may return a complex root even when a real root is also possible. |
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
609 Use @code{realpow}, @code{realsqrt}, @code{cbrt}, or @code{nthroot} if a |
14169
c0ca47ab7641
doc: Better explain which complex result is chosen by .^
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14149
diff
changeset
|
610 real result is preferred. |
3294 | 611 |
612 @item -@var{x} | |
613 @opindex - | |
614 Negation. | |
615 | |
616 @item +@var{x} | |
617 @opindex + | |
618 Unary plus. This operator has no effect on the operand. | |
619 | |
620 @item @var{x}' | |
621 @opindex ' | |
622 Complex conjugate transpose. For real arguments, this operator is the | |
623 same as the transpose operator. For complex arguments, this operator is | |
624 equivalent to the expression | |
625 | |
626 @example | |
627 conj (x.') | |
628 @end example | |
629 | |
630 @item @var{x}.' | |
631 @opindex .' | |
632 Transpose. | |
633 @end table | |
634 | |
14171
2ced2f59f523
doc: miscellaneous documentation improvements
Rik <octave@nomad.inbox5.com>
parents:
14169
diff
changeset
|
635 Note that because Octave's element-by-element operators begin with a |
3294 | 636 @samp{.}, there is a possible ambiguity for statements like |
637 | |
638 @example | |
639 1./m | |
640 @end example | |
641 | |
642 @noindent | |
643 because the period could be interpreted either as part of the constant | |
644 or as part of the operator. To resolve this conflict, Octave treats the | |
645 expression as if you had typed | |
646 | |
647 @example | |
648 (1) ./ m | |
649 @end example | |
650 | |
651 @noindent | |
652 and not | |
653 | |
654 @example | |
655 (1.) / m | |
656 @end example | |
657 | |
658 @noindent | |
659 Although this is inconsistent with the normal behavior of Octave's | |
660 lexer, which usually prefers to break the input into tokens by | |
661 preferring the longest possible match at any given point, it is more | |
662 useful in this case. | |
663 | |
11403
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
664 @opindex ' |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
665 @DOCSTRING(ctranspose) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
666 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
667 @opindex .\ |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
668 @DOCSTRING(ldivide) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
669 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
670 @opindex - |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
671 @DOCSTRING(minus) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
672 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
673 @opindex \ |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
674 @DOCSTRING(mldivide) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
675 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
676 @opindex ** |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
677 @opindex ^ |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
678 @DOCSTRING(mpower) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
679 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
680 @opindex / |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
681 @DOCSTRING(mrdivide) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
682 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
683 @opindex * |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
684 @DOCSTRING(mtimes) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
685 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
686 @opindex + |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
687 @DOCSTRING(plus) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
688 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
689 @opindex .** |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
690 @opindex .^ |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
691 @DOCSTRING(power) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
692 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
693 @opindex ./ |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
694 @DOCSTRING(rdivide) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
695 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
696 @opindex .* |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
697 @DOCSTRING(times) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
698 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
699 @opindex .' |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
700 @DOCSTRING(transpose) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
701 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
702 @opindex - |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
703 @DOCSTRING(uminus) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
704 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
705 @opindex + |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
706 @DOCSTRING(uplus) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
707 |
4167 | 708 @node Comparison Ops |
3294 | 709 @section Comparison Operators |
710 @cindex comparison expressions | |
711 @cindex expressions, comparison | |
712 @cindex relational operators | |
713 @cindex operators, relational | |
714 @cindex less than operator | |
715 @cindex greater than operator | |
716 @cindex equality operator | |
717 @cindex tests for equality | |
718 @cindex equality, tests for | |
719 | |
720 @dfn{Comparison operators} compare numeric values for relationships | |
721 such as equality. They are written using | |
722 @emph{relational operators}. | |
723 | |
724 All of Octave's comparison operators return a value of 1 if the | |
725 comparison is true, or 0 if it is false. For matrix values, they all | |
14116
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14038
diff
changeset
|
726 work on an element-by-element basis. Broadcasting rules apply. |
14119
94e2a76f1e5a
doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14116
diff
changeset
|
727 @xref{Broadcasting}. For example: |
3294 | 728 |
729 @example | |
730 @group | |
731 [1, 2; 3, 4] == [1, 3; 2, 4] | |
732 @result{} 1 0 | |
733 0 1 | |
734 @end group | |
735 @end example | |
736 | |
14116
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14038
diff
changeset
|
737 According to broadcasting rules, if one operand is a scalar and the |
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14038
diff
changeset
|
738 other is a matrix, the scalar is compared to each element of the matrix |
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14038
diff
changeset
|
739 in turn, and the result is the same size as the matrix. |
3294 | 740 |
741 @table @code | |
742 @item @var{x} < @var{y} | |
743 @opindex < | |
744 True if @var{x} is less than @var{y}. | |
745 | |
746 @item @var{x} <= @var{y} | |
747 @opindex <= | |
748 True if @var{x} is less than or equal to @var{y}. | |
749 | |
750 @item @var{x} == @var{y} | |
751 @opindex == | |
752 True if @var{x} is equal to @var{y}. | |
753 | |
754 @item @var{x} >= @var{y} | |
755 @opindex >= | |
756 True if @var{x} is greater than or equal to @var{y}. | |
757 | |
758 @item @var{x} > @var{y} | |
759 @opindex > | |
760 True if @var{x} is greater than @var{y}. | |
761 | |
17178
d6499c14021c
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
17160
diff
changeset
|
762 @item @var{x} != @var{y} |
3294 | 763 @itemx @var{x} ~= @var{y} |
764 @opindex != | |
765 @opindex ~= | |
766 True if @var{x} is not equal to @var{y}. | |
767 @end table | |
768 | |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
769 For complex numbers, the following ordering is defined: |
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
770 @var{z1} < @var{z2} |
16826
a4969508008e
doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents:
14856
diff
changeset
|
771 if and only if |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
772 |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
773 @example |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9578
diff
changeset
|
774 @group |
14856
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14171
diff
changeset
|
775 abs (@var{z1}) < abs (@var{z2}) |
c3fd61c59e9c
maint: Use Octave coding conventions for cuddling parentheses in doc directory
Rik <octave@nomad.inbox5.com>
parents:
14171
diff
changeset
|
776 || (abs (@var{z1}) == abs (@var{z2}) && arg (@var{z1}) < arg (@var{z2})) |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9578
diff
changeset
|
777 @end group |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
778 @end example |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
779 |
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
780 This is consistent with the ordering used by @dfn{max}, @dfn{min} and |
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
781 @dfn{sort}, but is not consistent with @sc{matlab}, which only compares the real |
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
782 parts. |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
783 |
3294 | 784 String comparisons may also be performed with the @code{strcmp} |
785 function, not with the comparison operators listed above. | |
786 @xref{Strings}. | |
787 | |
11403
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
788 @opindex == |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
789 @DOCSTRING(eq) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
790 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
791 @opindex >= |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
792 @DOCSTRING(ge) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
793 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
794 @opindex > |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
795 @DOCSTRING(gt) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
796 |
6550 | 797 @DOCSTRING(isequal) |
798 | |
16935
a7b2fc7fe1a9
binocdf.m: Reverse calling convention to betaincinv to preserve accuracy when p =~ 1.
Rik <rik@octave.org>
parents:
16826
diff
changeset
|
799 @DOCSTRING(isequaln) |
6550 | 800 |
11403
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
801 @opindex <= |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
802 @DOCSTRING(le) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
803 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
804 @opindex < |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
805 @DOCSTRING(lt) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
806 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
807 @opindex != |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
808 @opindex ~= |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
809 @DOCSTRING(ne) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
810 |
4167 | 811 @node Boolean Expressions |
3294 | 812 @section Boolean Expressions |
813 @cindex expressions, boolean | |
814 @cindex boolean expressions | |
815 @cindex expressions, logical | |
816 @cindex logical expressions | |
817 @cindex operators, boolean | |
818 @cindex boolean operators | |
819 @cindex logical operators | |
820 @cindex operators, logical | |
821 @cindex and operator | |
822 @cindex or operator | |
823 @cindex not operator | |
824 | |
825 @menu | |
17160
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
826 * Element-by-element Boolean Operators:: |
f2a8592b8fbd
doc: Shorten some long subsection names in Manual.
Rik <rik@octave.org>
parents:
16935
diff
changeset
|
827 * Short-circuit Boolean Operators:: |
3294 | 828 @end menu |
829 | |
4167 | 830 @node Element-by-element Boolean Operators |
3294 | 831 @subsection Element-by-element Boolean Operators |
832 @cindex element-by-element evaluation | |
833 | |
834 An @dfn{element-by-element boolean expression} is a combination of | |
835 comparison expressions using the boolean | |
836 operators ``or'' (@samp{|}), ``and'' (@samp{&}), and ``not'' (@samp{!}), | |
837 along with parentheses to control nesting. The truth of the boolean | |
838 expression is computed by combining the truth values of the | |
839 corresponding elements of the component expressions. A value is | |
840 considered to be false if it is zero, and true otherwise. | |
841 | |
842 Element-by-element boolean expressions can be used wherever comparison | |
843 expressions can be used. They can be used in @code{if} and @code{while} | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9159
diff
changeset
|
844 statements. However, a matrix value used as the condition in an |
3294 | 845 @code{if} or @code{while} statement is only true if @emph{all} of its |
846 elements are nonzero. | |
847 | |
848 Like comparison operations, each element of an element-by-element | |
849 boolean expression also has a numeric value (1 if true, 0 if false) that | |
850 comes into play if the result of the boolean expression is stored in a | |
851 variable, or used in arithmetic. | |
852 | |
853 Here are descriptions of the three element-by-element boolean operators. | |
854 | |
855 @table @code | |
856 @item @var{boolean1} & @var{boolean2} | |
857 @opindex & | |
858 Elements of the result are true if both corresponding elements of | |
859 @var{boolean1} and @var{boolean2} are true. | |
860 | |
861 @item @var{boolean1} | @var{boolean2} | |
862 @opindex | | |
863 Elements of the result are true if either of the corresponding elements | |
864 of @var{boolean1} or @var{boolean2} is true. | |
865 | |
17178
d6499c14021c
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
17160
diff
changeset
|
866 @item ! @var{boolean} |
3294 | 867 @itemx ~ @var{boolean} |
868 @opindex ~ | |
869 @opindex ! | |
870 Each element of the result is true if the corresponding element of | |
871 @var{boolean} is false. | |
872 @end table | |
873 | |
14119
94e2a76f1e5a
doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14116
diff
changeset
|
874 These operators work on an element-by-element basis. For example, the |
14116
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14038
diff
changeset
|
875 expression |
3294 | 876 |
877 @example | |
878 [1, 0; 0, 1] & [1, 0; 2, 3] | |
879 @end example | |
880 | |
881 @noindent | |
882 returns a two by two identity matrix. | |
883 | |
14119
94e2a76f1e5a
doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14116
diff
changeset
|
884 For the binary operators, broadcasting rules apply. @xref{Broadcasting}. |
14116
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14038
diff
changeset
|
885 In particular, if one of the operands is a scalar and the other a |
3294 | 886 matrix, the operator is applied to the scalar and each element of the |
887 matrix. | |
888 | |
889 For the binary element-by-element boolean operators, both subexpressions | |
890 @var{boolean1} and @var{boolean2} are evaluated before computing the | |
891 result. This can make a difference when the expressions have side | |
892 effects. For example, in the expression | |
893 | |
894 @example | |
895 a & b++ | |
896 @end example | |
897 | |
898 @noindent | |
899 the value of the variable @var{b} is incremented even if the variable | |
900 @var{a} is zero. | |
901 | |
902 This behavior is necessary for the boolean operators to work as | |
903 described for matrix-valued operands. | |
904 | |
11403
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
905 @opindex & |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
906 @DOCSTRING(and) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
907 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
908 @opindex ~ |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
909 @opindex ! |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
910 @DOCSTRING(not) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
911 |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
912 @opindex | |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
913 @DOCSTRING(or) |
b8b08b1ac21f
Add missing operator functions to doc/interpreter.
Judd Storrs <jstorrs@gmail.com>
parents:
10846
diff
changeset
|
914 |
4167 | 915 @node Short-circuit Boolean Operators |
3294 | 916 @subsection Short-circuit Boolean Operators |
917 @cindex short-circuit evaluation | |
918 | |
919 Combined with the implicit conversion to scalar values in @code{if} and | |
920 @code{while} conditions, Octave's element-by-element boolean operators | |
921 are often sufficient for performing most logical operations. However, | |
922 it is sometimes desirable to stop evaluating a boolean expression as | |
923 soon as the overall truth value can be determined. Octave's | |
924 @dfn{short-circuit} boolean operators work this way. | |
925 | |
926 @table @code | |
927 @item @var{boolean1} && @var{boolean2} | |
928 @opindex && | |
929 The expression @var{boolean1} is evaluated and converted to a scalar | |
6632 | 930 using the equivalent of the operation @code{all (@var{boolean1}(:))}. |
3294 | 931 If it is false, the result of the overall expression is 0. If it is |
932 true, the expression @var{boolean2} is evaluated and converted to a | |
6632 | 933 scalar using the equivalent of the operation @code{all |
934 (@var{boolean1}(:))}. If it is true, the result of the overall expression | |
3294 | 935 is 1. Otherwise, the result of the overall expression is 0. |
936 | |
6632 | 937 @strong{Warning:} there is one exception to the rule of evaluating |
938 @code{all (@var{boolean1}(:))}, which is when @code{boolean1} is the | |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
939 empty matrix. The truth value of an empty matrix is always @code{false} |
6632 | 940 so @code{[] && true} evaluates to @code{false} even though |
941 @code{all ([])} is @code{true}. | |
942 | |
3294 | 943 @item @var{boolean1} || @var{boolean2} |
944 @opindex || | |
945 The expression @var{boolean1} is evaluated and converted to a scalar | |
6632 | 946 using the equivalent of the operation @code{all (@var{boolean1}(:))}. |
3294 | 947 If it is true, the result of the overall expression is 1. If it is |
948 false, the expression @var{boolean2} is evaluated and converted to a | |
6632 | 949 scalar using the equivalent of the operation @code{all |
950 (@var{boolean1}(:))}. If it is true, the result of the overall expression | |
3294 | 951 is 1. Otherwise, the result of the overall expression is 0. |
6632 | 952 |
953 @strong{Warning:} the truth value of an empty matrix is always @code{false}, | |
954 see the previous list item for details. | |
3294 | 955 @end table |
956 | |
957 The fact that both operands may not be evaluated before determining the | |
958 overall truth value of the expression can be important. For example, in | |
959 the expression | |
960 | |
961 @example | |
962 a && b++ | |
963 @end example | |
964 | |
965 @noindent | |
966 the value of the variable @var{b} is only incremented if the variable | |
967 @var{a} is nonzero. | |
968 | |
969 This can be used to write somewhat more concise code. For example, it | |
970 is possible write | |
971 | |
972 @example | |
973 @group | |
974 function f (a, b, c) | |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7594
diff
changeset
|
975 if (nargin > 2 && ischar (c)) |
3294 | 976 @dots{} |
977 @end group | |
978 @end example | |
979 | |
980 @noindent | |
981 instead of having to use two @code{if} statements to avoid attempting to | |
982 evaluate an argument that doesn't exist. For example, without the | |
983 short-circuit feature, it would be necessary to write | |
984 | |
985 @example | |
986 @group | |
987 function f (a, b, c) | |
988 if (nargin > 2) | |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7594
diff
changeset
|
989 if (ischar (c)) |
3294 | 990 @dots{} |
991 @end group | |
992 @end example | |
993 | |
6632 | 994 @noindent |
3294 | 995 Writing |
996 | |
997 @example | |
998 @group | |
999 function f (a, b, c) | |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7594
diff
changeset
|
1000 if (nargin > 2 & ischar (c)) |
3294 | 1001 @dots{} |
1002 @end group | |
1003 @end example | |
1004 | |
1005 @noindent | |
1006 would result in an error if @code{f} were called with one or two | |
1007 arguments because Octave would be forced to try to evaluate both of the | |
1008 operands for the operator @samp{&}. | |
1009 | |
12543
f60f755ebfe4
Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11552
diff
changeset
|
1010 @sc{matlab} has special behavior that allows the operators @samp{&} and |
f60f755ebfe4
Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11552
diff
changeset
|
1011 @samp{|} to short-circuit when used in the truth expression for @code{if} and |
f60f755ebfe4
Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11552
diff
changeset
|
1012 @code{while} statements. The Octave parser may be instructed to behave in the |
12601
99babbf683ff
doc: Correct use of it's -> its in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12546
diff
changeset
|
1013 same manner, but its use is strongly discouraged. |
12543
f60f755ebfe4
Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11552
diff
changeset
|
1014 |
f60f755ebfe4
Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11552
diff
changeset
|
1015 @DOCSTRING(do_braindead_shortcircuit_evaluation) |
f60f755ebfe4
Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11552
diff
changeset
|
1016 |
f60f755ebfe4
Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11552
diff
changeset
|
1017 Finally, the ternary operator (?:) is not supported in Octave. If |
f60f755ebfe4
Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11552
diff
changeset
|
1018 short-circuiting is not important, it can be replaced by the @code{ifelse} |
f60f755ebfe4
Add do_braindead_shortcircuit_evaluation to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11552
diff
changeset
|
1019 function. |
10308 | 1020 |
11552
6b6e9051ecb8
Add merge/ifelse function to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
1021 @DOCSTRING(merge) |
10308 | 1022 |
4167 | 1023 @node Assignment Ops |
3294 | 1024 @section Assignment Expressions |
1025 @cindex assignment expressions | |
1026 @cindex assignment operators | |
1027 @cindex operators, assignment | |
1028 @cindex expressions, assignment | |
1029 | |
1030 @opindex = | |
1031 | |
1032 An @dfn{assignment} is an expression that stores a new value into a | |
1033 variable. For example, the following expression assigns the value 1 to | |
1034 the variable @code{z}: | |
1035 | |
1036 @example | |
1037 z = 1 | |
1038 @end example | |
1039 | |
6632 | 1040 @noindent |
3294 | 1041 After this expression is executed, the variable @code{z} has the value 1. |
1042 Whatever old value @code{z} had before the assignment is forgotten. | |
1043 The @samp{=} sign is called an @dfn{assignment operator}. | |
1044 | |
1045 Assignments can store string values also. For example, the following | |
17289
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17178
diff
changeset
|
1046 expression would store the value @qcode{"this food is good"} in the |
3294 | 1047 variable @code{message}: |
1048 | |
1049 @example | |
1050 @group | |
1051 thing = "food" | |
1052 predicate = "good" | |
1053 message = [ "this " , thing , " is " , predicate ] | |
1054 @end group | |
1055 @end example | |
1056 | |
1057 @noindent | |
1058 (This also illustrates concatenation of strings.) | |
1059 | |
1060 @cindex side effect | |
1061 Most operators (addition, concatenation, and so on) have no effect | |
1062 except to compute a value. If you ignore the value, you might as well | |
1063 not use the operator. An assignment operator is different. It does | |
1064 produce a value, but even if you ignore the value, the assignment still | |
1065 makes itself felt through the alteration of the variable. We call this | |
1066 a @dfn{side effect}. | |
1067 | |
1068 @cindex lvalue | |
1069 The left-hand operand of an assignment need not be a variable | |
1070 (@pxref{Variables}). It can also be an element of a matrix | |
1071 (@pxref{Index Expressions}) or a list of return values | |
1072 (@pxref{Calling Functions}). These are all called @dfn{lvalues}, which | |
1073 means they can appear on the left-hand side of an assignment operator. | |
1074 The right-hand operand may be any expression. It produces the new value | |
1075 which the assignment stores in the specified variable, matrix element, | |
1076 or list of return values. | |
1077 | |
1078 It is important to note that variables do @emph{not} have permanent types. | |
1079 The type of a variable is simply the type of whatever value it happens | |
1080 to hold at the moment. In the following program fragment, the variable | |
1081 @code{foo} has a numeric value at first, and a string value later on: | |
1082 | |
1083 @example | |
1084 @group | |
1085 octave:13> foo = 1 | |
1086 foo = 1 | |
1087 octave:13> foo = "bar" | |
1088 foo = bar | |
1089 @end group | |
1090 @end example | |
1091 | |
1092 @noindent | |
1093 When the second assignment gives @code{foo} a string value, the fact that | |
1094 it previously had a numeric value is forgotten. | |
1095 | |
1096 Assignment of a scalar to an indexed matrix sets all of the elements | |
1097 that are referenced by the indices to the scalar value. For example, if | |
1098 @code{a} is a matrix with at least two columns, | |
1099 | |
1100 @example | |
1101 @group | |
1102 a(:, 2) = 5 | |
1103 @end group | |
1104 @end example | |
1105 | |
1106 @noindent | |
1107 sets all the elements in the second column of @code{a} to 5. | |
1108 | |
1109 Assigning an empty matrix @samp{[]} works in most cases to allow you to | |
1110 delete rows or columns of matrices and vectors. @xref{Empty Matrices}. | |
1111 For example, given a 4 by 5 matrix @var{A}, the assignment | |
1112 | |
1113 @example | |
1114 A (3, :) = [] | |
1115 @end example | |
1116 | |
1117 @noindent | |
1118 deletes the third row of @var{A}, and the assignment | |
1119 | |
1120 @example | |
1121 A (:, 1:2:5) = [] | |
1122 @end example | |
1123 | |
1124 @noindent | |
6672 | 1125 deletes the first, third, and fifth columns. |
3294 | 1126 |
1127 An assignment is an expression, so it has a value. Thus, @code{z = 1} | |
1128 as an expression has the value 1. One consequence of this is that you | |
1129 can write multiple assignments together: | |
1130 | |
1131 @example | |
1132 x = y = z = 0 | |
1133 @end example | |
1134 | |
1135 @noindent | |
1136 stores the value 0 in all three variables. It does this because the | |
1137 value of @code{z = 0}, which is 0, is stored into @code{y}, and then | |
1138 the value of @code{y = z = 0}, which is 0, is stored into @code{x}. | |
1139 | |
1140 This is also true of assignments to lists of values, so the following is | |
1141 a valid expression | |
1142 | |
1143 @example | |
1144 [a, b, c] = [u, s, v] = svd (a) | |
1145 @end example | |
1146 | |
1147 @noindent | |
1148 that is exactly equivalent to | |
1149 | |
1150 @example | |
1151 @group | |
1152 [u, s, v] = svd (a) | |
1153 a = u | |
1154 b = s | |
1155 c = v | |
1156 @end group | |
1157 @end example | |
1158 | |
1159 In expressions like this, the number of values in each part of the | |
1160 expression need not match. For example, the expression | |
1161 | |
1162 @example | |
1163 [a, b] = [u, s, v] = svd (a) | |
1164 @end example | |
1165 | |
1166 @noindent | |
1167 is equivalent to | |
1168 | |
1169 @example | |
1170 @group | |
1171 [u, s, v] = svd (a) | |
1172 a = u | |
1173 b = s | |
1174 @end group | |
1175 @end example | |
1176 | |
6632 | 1177 @noindent |
1178 The number of values on the left side of the expression can, however, | |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
1179 not exceed the number of values on the right side. For example, the |
6632 | 1180 following will produce an error. |
1181 | |
9153
5247e89688e1
Eliminate most overfull errors when running texi2pdf for generating pdf documentation
Rik <rdrider0-list@yahoo.com>
parents:
9037
diff
changeset
|
1182 @example |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9159
diff
changeset
|
1183 @group |
8015
30629059b72d
Update the manual to reflect the changes in error output
sh@sh-laptop
parents:
7768
diff
changeset
|
1184 [a, b, c, d] = [u, s, v] = svd (a); |
7031 | 1185 @print{} error: element number 4 undefined in return list |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9159
diff
changeset
|
1186 @end group |
9153
5247e89688e1
Eliminate most overfull errors when running texi2pdf for generating pdf documentation
Rik <rdrider0-list@yahoo.com>
parents:
9037
diff
changeset
|
1187 @end example |
6632 | 1188 |
10209
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1189 The symbol @code{~} may be used as a placeholder in the list of lvalues, |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1190 indicating that the corresponding return value should be ignored and not stored |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1191 anywhere: |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1192 |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1193 @example |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1194 @group |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1195 [~, s, v] = svd (a); |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1196 @end group |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1197 @end example |
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1198 |
10228 | 1199 This is cleaner and more memory efficient than using a dummy variable. |
1200 The @code{nargout} value for the right-hand side expression is not affected. | |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
1201 If the assignment is used as an expression, the return value is a |
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
1202 comma-separated list with the ignored values dropped. |
10209
ea0d83b4470b
document use of ~ placeholders in the manual
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
1203 |
6642 | 1204 @opindex += |
1205 A very common programming pattern is to increment an existing variable | |
1206 with a given value, like this | |
1207 | |
1208 @example | |
1209 a = a + 2; | |
1210 @end example | |
1211 | |
1212 @noindent | |
1213 This can be written in a clearer and more condensed form using the | |
1214 @code{+=} operator | |
1215 | |
1216 @example | |
1217 a += 2; | |
1218 @end example | |
1219 | |
1220 @noindent | |
1221 @opindex -= | |
1222 @opindex *= | |
1223 @opindex /= | |
1224 Similar operators also exist for subtraction (@code{-=}), | |
9037
4cb9f994dcec
Documentation cleanup of var.texi, expr.texi, eval.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
1225 multiplication (@code{*=}), and division (@code{/=}). An expression |
6642 | 1226 of the form |
1227 | |
1228 @example | |
1229 @var{expr1} @var{op}= @var{expr2} | |
1230 @end example | |
1231 | |
1232 @noindent | |
1233 is evaluated as | |
1234 | |
1235 @example | |
1236 @var{expr1} = (@var{expr1}) @var{op} (@var{expr2}) | |
1237 @end example | |
1238 | |
1239 @noindent | |
1240 where @var{op} can be either @code{+}, @code{-}, @code{*}, or @code{/}. | |
1241 So, the expression | |
1242 | |
1243 @example | |
1244 a *= b+1 | |
1245 @end example | |
1246 | |
1247 @noindent | |
1248 is evaluated as | |
1249 | |
1250 @example | |
1251 a = a * (b+1) | |
1252 @end example | |
1253 | |
1254 @noindent | |
1255 and @emph{not} | |
1256 | |
1257 @example | |
1258 a = a * b + 1 | |
1259 @end example | |
1260 | |
3294 | 1261 You can use an assignment anywhere an expression is called for. For |
1262 example, it is valid to write @code{x != (y = 1)} to set @code{y} to 1 | |
1263 and then test whether @code{x} equals 1. But this style tends to make | |
1264 programs hard to read. Except in a one-shot program, you should rewrite | |
1265 it to get rid of such nesting of assignments. This is never very hard. | |
1266 | |
1267 @cindex increment operator | |
1268 @cindex decrement operator | |
1269 @cindex operators, increment | |
1270 @cindex operators, decrement | |
1271 | |
4167 | 1272 @node Increment Ops |
3294 | 1273 @section Increment Operators |
1274 | |
1275 @emph{Increment operators} increase or decrease the value of a variable | |
1276 by 1. The operator to increment a variable is written as @samp{++}. It | |
1277 may be used to increment a variable either before or after taking its | |
1278 value. | |
1279 | |
1280 For example, to pre-increment the variable @var{x}, you would write | |
1281 @code{++@var{x}}. This would add one to @var{x} and then return the new | |
1282 value of @var{x} as the result of the expression. It is exactly the | |
1283 same as the expression @code{@var{x} = @var{x} + 1}. | |
1284 | |
1285 To post-increment a variable @var{x}, you would write @code{@var{x}++}. | |
1286 This adds one to the variable @var{x}, but returns the value that | |
1287 @var{x} had prior to incrementing it. For example, if @var{x} is equal | |
1288 to 2, the result of the expression @code{@var{x}++} is 2, and the new | |
1289 value of @var{x} is 3. | |
1290 | |
1291 For matrix and vector arguments, the increment and decrement operators | |
1292 work on each element of the operand. | |
1293 | |
1294 Here is a list of all the increment and decrement expressions. | |
1295 | |
1296 @table @code | |
1297 @item ++@var{x} | |
1298 @opindex ++ | |
1299 This expression increments the variable @var{x}. The value of the | |
1300 expression is the @emph{new} value of @var{x}. It is equivalent to the | |
1301 expression @code{@var{x} = @var{x} + 1}. | |
1302 | |
1303 @item --@var{x} | |
1304 @opindex @code{--} | |
1305 This expression decrements the variable @var{x}. The value of the | |
1306 expression is the @emph{new} value of @var{x}. It is equivalent to the | |
1307 expression @code{@var{x} = @var{x} - 1}. | |
1308 | |
1309 @item @var{x}++ | |
1310 @opindex ++ | |
1311 This expression causes the variable @var{x} to be incremented. The | |
1312 value of the expression is the @emph{old} value of @var{x}. | |
1313 | |
1314 @item @var{x}-- | |
1315 @opindex @code{--} | |
1316 This expression causes the variable @var{x} to be decremented. The | |
1317 value of the expression is the @emph{old} value of @var{x}. | |
1318 @end table | |
1319 | |
4167 | 1320 @node Operator Precedence |
3294 | 1321 @section Operator Precedence |
1322 @cindex operator precedence | |
1323 | |
1324 @dfn{Operator precedence} determines how operators are grouped, when | |
1325 different operators appear close by in one expression. For example, | |
1326 @samp{*} has higher precedence than @samp{+}. Thus, the expression | |
1327 @code{a + b * c} means to multiply @code{b} and @code{c}, and then add | |
1328 @code{a} to the product (i.e., @code{a + (b * c)}). | |
1329 | |
1330 You can overrule the precedence of the operators by using parentheses. | |
1331 You can think of the precedence rules as saying where the parentheses | |
1332 are assumed if you do not write parentheses yourself. In fact, it is | |
1333 wise to use parentheses whenever you have an unusual combination of | |
1334 operators, because other people who read the program may not remember | |
1335 what the precedence is in this case. You might forget as well, and then | |
1336 you too could make a mistake. Explicit parentheses will help prevent | |
1337 any such mistake. | |
1338 | |
1339 When operators of equal precedence are used together, the leftmost | |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1340 operator groups first, except for the assignment operators, which group |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1341 in the opposite order. Thus, the expression @code{a - b + c} groups as |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1342 @code{(a - b) + c}, but the expression @code{a = b = c} groups as |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1343 @code{a = (b = c)}. |
3294 | 1344 |
1345 The precedence of prefix unary operators is important when another | |
1346 operator follows the operand. For example, @code{-x^2} means | |
1347 @code{-(x^2)}, because @samp{-} has lower precedence than @samp{^}. | |
1348 | |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1349 Here is a table of the operators in Octave, in order of decreasing |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1350 precedence. Unless noted, all operators group left to right. |
3294 | 1351 |
1352 @table @code | |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1353 @item function call and array indexing, cell array indexing, and structure element indexing |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1354 @samp{()} @samp{@{@}} @samp{.} |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1355 |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1356 @item postfix increment, and postfix decrement |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1357 @samp{++} @samp{--} |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1358 |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1359 These operators group right to left. |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1360 |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1361 @item transpose and exponentiation |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1362 @samp{'} @samp{.'} @samp{^} @samp{**} @samp{.^} @samp{.**} |
3294 | 1363 |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1364 @item unary plus, unary minus, prefix increment, prefix decrement, and logical "not" |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1365 @samp{+} @samp{-} @samp{++} @samp{--} @samp{~} @samp{!} |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1366 |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1367 @item multiply and divide |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1368 @samp{*} @samp{/} @samp{\} @samp{.\} @samp{.*} @samp{./} |
3294 | 1369 |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1370 @item add, subtract |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1371 @samp{+} @samp{-} |
3294 | 1372 |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1373 @item colon |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1374 @samp{:} |
3294 | 1375 |
1376 @item relational | |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1377 @samp{<} @samp{<=} @samp{==} @samp{>=} @samp{>} @samp{!=} |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1378 @samp{~=} |
3294 | 1379 |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1380 @item element-wise "and" |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1381 @samp{&} |
3294 | 1382 |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1383 @item element-wise "or" |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1384 @samp{|} |
3294 | 1385 |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1386 @item logical "and" |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1387 @samp{&&} |
3294 | 1388 |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1389 @item logical "or" |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1390 @samp{||} |
3294 | 1391 |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1392 @item assignment |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1393 @samp{=} @samp{+=} @samp{-=} @samp{*=} @samp{/=} @samp{\=} |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1394 @samp{^=} @samp{.*=} @samp{./=} @samp{.\=} @samp{.^=} @samp{|=} |
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1395 @samp{&=} |
3294 | 1396 |
12627
002948ae5bc0
fix precedence level of transpose operators (bug #32533)
John W. Eaton <jwe@octave.org>
parents:
12601
diff
changeset
|
1397 These operators group right to left. |
3294 | 1398 @end table |