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