Mercurial > hg > octave-nkf
annotate doc/interpreter/matrix.txi @ 11523:fd0a3ac60b0e
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 14 Jan 2011 05:47:45 -0500 |
parents | 0d9640d755b1 |
children | 6bac61388876 |
rev | line source |
---|---|
11523 | 1 @c Copyright (C) 1996-2011 John W. Eaton |
7018 | 2 @c |
3 @c This file is part of Octave. | |
4 @c | |
5 @c Octave is free software; you can redistribute it and/or modify it | |
6 @c under the terms of the GNU General Public License as published by the | |
7 @c Free Software Foundation; either version 3 of the License, or (at | |
8 @c your option) any later version. | |
9 @c | |
10 @c Octave is distributed in the hope that it will be useful, but WITHOUT | |
11 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 @c for more details. | |
14 @c | |
15 @c You should have received a copy of the GNU General Public License | |
16 @c along with Octave; see the file COPYING. If not, see | |
17 @c <http://www.gnu.org/licenses/>. | |
3294 | 18 |
4167 | 19 @node Matrix Manipulation |
3294 | 20 @chapter Matrix Manipulation |
21 | |
22 There are a number of functions available for checking to see if the | |
23 elements of a matrix meet some condition, and for rearranging the | |
24 elements of a matrix. For example, Octave can easily tell you if all | |
25 the elements of a matrix are finite, or are less than some specified | |
26 value. Octave can also rotate the elements, extract the upper- or | |
27 lower-triangular parts, or sort the columns of a matrix. | |
28 | |
29 @menu | |
30 * Finding Elements and Checking Conditions:: | |
31 * Rearranging Matrices:: | |
6550 | 32 * Applying a Function to an Array:: |
3294 | 33 * Special Utility Matrices:: |
34 * Famous Matrices:: | |
35 @end menu | |
36 | |
4167 | 37 @node Finding Elements and Checking Conditions |
3294 | 38 @section Finding Elements and Checking Conditions |
39 | |
40 The functions @code{any} and @code{all} are useful for determining | |
41 whether any or all of the elements of a matrix satisfy some condition. | |
42 The @code{find} function is also useful in determining which elements of | |
43 a matrix meet a specified condition. | |
44 | |
3369 | 45 @DOCSTRING(any) |
3294 | 46 |
3369 | 47 @DOCSTRING(all) |
3294 | 48 |
49 Since the comparison operators (@pxref{Comparison Ops}) return matrices | |
50 of ones and zeros, it is easy to test a matrix for many things, not just | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10828
diff
changeset
|
51 whether the elements are nonzero. For example, |
3294 | 52 |
53 @example | |
54 @group | |
55 all (all (rand (5) < 0.9)) | |
56 @result{} 0 | |
57 @end group | |
58 @end example | |
59 | |
60 @noindent | |
61 tests a random 5 by 5 matrix to see if all of its elements are less | |
62 than 0.9. | |
63 | |
64 Note that in conditional contexts (like the test clause of @code{if} and | |
65 @code{while} statements) Octave treats the test as if you had typed | |
66 @code{all (all (condition))}. | |
67 | |
3428 | 68 @DOCSTRING(xor) |
69 | |
70 @DOCSTRING(is_duplicate_entry) | |
3294 | 71 |
3369 | 72 @DOCSTRING(diff) |
3294 | 73 |
3369 | 74 @DOCSTRING(isinf) |
3294 | 75 |
3369 | 76 @DOCSTRING(isnan) |
3294 | 77 |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10892
diff
changeset
|
78 @DOCSTRING(isfinite) |
3294 | 79 |
3369 | 80 @DOCSTRING(find) |
3294 | 81 |
9367 | 82 @DOCSTRING(lookup) |
83 | |
3428 | 84 @DOCSTRING(common_size) |
85 | |
4167 | 86 @node Rearranging Matrices |
3294 | 87 @section Rearranging Matrices |
88 | |
3369 | 89 @DOCSTRING(fliplr) |
3294 | 90 |
3369 | 91 @DOCSTRING(flipud) |
3294 | 92 |
4869 | 93 @DOCSTRING(flipdim) |
94 | |
3369 | 95 @DOCSTRING(rot90) |
3294 | 96 |
4869 | 97 @DOCSTRING(rotdim) |
98 | |
4845 | 99 @DOCSTRING(cat) |
100 | |
101 @DOCSTRING(horzcat) | |
102 | |
103 @DOCSTRING(vertcat) | |
104 | |
105 @DOCSTRING(permute) | |
106 | |
107 @DOCSTRING(ipermute) | |
108 | |
3369 | 109 @DOCSTRING(reshape) |
3294 | 110 |
8432
c9cb8f0b8b4f
add reference to resize function in the manual
Francesco Potortì <pot@gnu.org>
parents:
8286
diff
changeset
|
111 @DOCSTRING(resize) |
c9cb8f0b8b4f
add reference to resize function in the manual
Francesco Potortì <pot@gnu.org>
parents:
8286
diff
changeset
|
112 |
4894 | 113 @DOCSTRING(circshift) |
114 | |
115 @DOCSTRING(shiftdim) | |
116 | |
3369 | 117 @DOCSTRING(shift) |
3294 | 118 |
3369 | 119 @DOCSTRING(sort) |
3294 | 120 |
6550 | 121 @DOCSTRING(sortrows) |
122 | |
8817
03b7f618ab3d
include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents:
8519
diff
changeset
|
123 @DOCSTRING(issorted) |
03b7f618ab3d
include docstrings for new functions in the manual
John W. Eaton <jwe@octave.org>
parents:
8519
diff
changeset
|
124 |
3294 | 125 Since the @code{sort} function does not allow sort keys to be specified, |
126 it can't be used to order the rows of a matrix according to the values | |
127 of the elements in various columns@footnote{For example, to first sort | |
128 based on the values in column 1, and then, for any values that are | |
129 repeated in column 1, sort based on the values found in column 2, etc.} | |
130 in a single call. Using the second output, however, it is possible to | |
131 sort all rows based on the values in a given column. Here's an example | |
132 that sorts the rows of a matrix based on the values in the second | |
133 column. | |
134 | |
135 @example | |
136 @group | |
137 a = [1, 2; 2, 3; 3, 1]; | |
138 [s, i] = sort (a (:, 2)); | |
139 a (i, :) | |
140 @result{} 3 1 | |
141 1 2 | |
142 2 3 | |
143 @end group | |
144 @end example | |
145 | |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8133
diff
changeset
|
146 @anchor{doc-triu} |
3369 | 147 @DOCSTRING(tril) |
3294 | 148 |
3369 | 149 @DOCSTRING(vec) |
3294 | 150 |
3369 | 151 @DOCSTRING(vech) |
3294 | 152 |
8512
137d6c94212f
fix missing anchor in matrix.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8432
diff
changeset
|
153 @anchor{doc-postpad} |
3428 | 154 @DOCSTRING(prepad) |
155 | |
8851
d6de39523f03
improve the diag & perm matrices text
Jaroslav Hajek <highegg@gmail.com>
parents:
8839
diff
changeset
|
156 @DOCSTRING(diag) |
d6de39523f03
improve the diag & perm matrices text
Jaroslav Hajek <highegg@gmail.com>
parents:
8839
diff
changeset
|
157 |
6550 | 158 @DOCSTRING(blkdiag) |
159 | |
160 @node Applying a Function to an Array | |
161 @section Applying a Function to an Array | |
162 | |
163 @DOCSTRING(arrayfun) | |
164 | |
6868 | 165 @DOCSTRING(bsxfun) |
166 | |
4167 | 167 @node Special Utility Matrices |
3294 | 168 @section Special Utility Matrices |
169 | |
3369 | 170 @DOCSTRING(eye) |
3294 | 171 |
3369 | 172 @DOCSTRING(ones) |
3294 | 173 |
3369 | 174 @DOCSTRING(zeros) |
3294 | 175 |
3920 | 176 @DOCSTRING(repmat) |
177 | |
10801
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
178 @DOCSTRING(repelems) |
a40e32927b3a
Improve documentation for new repelems function.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
179 |
3369 | 180 @DOCSTRING(rand) |
3294 | 181 |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
182 @DOCSTRING(randi) |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
183 |
3369 | 184 @DOCSTRING(randn) |
3294 | 185 |
5730 | 186 @DOCSTRING(rande) |
187 | |
188 @DOCSTRING(randp) | |
189 | |
190 @DOCSTRING(randg) | |
191 | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
192 The generators operate in the new or old style together, it is not |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
193 possible to mix the two. Initializing any generator with |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
194 @code{"state"} or @code{"seed"} causes the others to switch to the |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
195 same style for future calls. |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
196 |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
197 The state of each generator is independent and calls to different |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
198 generators can be interleaved without affecting the final result. For |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
199 example, |
3294 | 200 |
201 @example | |
202 @group | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
203 rand ("state", [11, 22, 33]); |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
204 randn ("state", [44, 55, 66]); |
3294 | 205 u = rand (100, 1); |
206 n = randn (100, 1); | |
207 @end group | |
208 @end example | |
209 | |
210 @noindent | |
211 and | |
212 | |
213 @example | |
214 @group | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
215 rand ("state", [11, 22, 33]); |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
216 randn ("state", [44, 55, 66]); |
3294 | 217 u = zeros (100, 1); |
218 n = zeros (100, 1); | |
219 for i = 1:100 | |
220 u(i) = rand (); | |
221 n(i) = randn (); | |
222 end | |
223 @end group | |
224 @end example | |
225 | |
226 @noindent | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
227 produce equivalent results. When the generators are initialized in |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
228 the old style with @code{"seed"} only @code{rand} and @code{randn} are |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
229 independent, because the old @code{rande}, @code{randg} and |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
230 @code{randp} generators make calls to @code{rand} and @code{randn}. |
3294 | 231 |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
232 The generators are initialized with random states at start-up, so |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
233 that the sequences of random numbers are not the same each time you run |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
234 Octave.@footnote{The old versions of @code{rand} and @code{randn} |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
235 obtain their initial seeds from the system clock.} If you really do |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
236 need to reproduce a sequence of numbers exactly, you can set the state |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
237 or seed to a specific value. |
3294 | 238 |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
239 If invoked without arguments, @code{rand} and @code{randn} return a |
3294 | 240 single element of a random sequence. |
241 | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
242 The original @code{rand} and @code{randn} functions use Fortran code from |
10828
322f43e0e170
Grammarcheck .txi documentation files.
Rik <octave@nomad.inbox5.com>
parents:
10801
diff
changeset
|
243 @sc{ranlib}, a library of Fortran routines for random number generation, |
3294 | 244 compiled by Barry W. Brown and James Lovato of the Department of |
245 Biomathematics at The University of Texas, M.D. Anderson Cancer Center, | |
246 Houston, TX 77030. | |
247 | |
3428 | 248 @DOCSTRING(randperm) |
249 | |
3294 | 250 The functions @code{linspace} and @code{logspace} make it very easy to |
251 create vectors with evenly or logarithmically spaced elements. | |
252 @xref{Ranges}. | |
253 | |
3369 | 254 @DOCSTRING(linspace) |
3294 | 255 |
3369 | 256 @DOCSTRING(logspace) |
3294 | 257 |
4167 | 258 @node Famous Matrices |
3294 | 259 @section Famous Matrices |
260 | |
261 The following functions return famous matrix forms. | |
262 | |
6502 | 263 @DOCSTRING(hadamard) |
264 | |
3369 | 265 @DOCSTRING(hankel) |
3294 | 266 |
3369 | 267 @DOCSTRING(hilb) |
3294 | 268 |
3369 | 269 @DOCSTRING(invhilb) |
3294 | 270 |
6502 | 271 @DOCSTRING(magic) |
272 | |
273 @DOCSTRING(pascal) | |
274 | |
275 @DOCSTRING(rosser) | |
276 | |
3369 | 277 @DOCSTRING(sylvester_matrix) |
3294 | 278 |
3369 | 279 @DOCSTRING(toeplitz) |
3294 | 280 |
3369 | 281 @DOCSTRING(vander) |
6502 | 282 |
283 @DOCSTRING(wilkinson) |