Mercurial > hg > octave-nkf
annotate doc/interpreter/matrix.txi @ 8512:137d6c94212f
fix missing anchor in matrix.txi
author | Thorsten Meyer <thorsten.meyier@gmx.de> |
---|---|
date | Wed, 14 Jan 2009 15:38:24 -0500 |
parents | c9cb8f0b8b4f |
children | 68aa5abfd136 |
rev | line source |
---|---|
6778 | 1 @c Copyright (C) 1996, 1997, 2007 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 | |
51 whether the elements are nonzero. For example, | |
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 |
3369 | 78 @DOCSTRING(finite) |
3294 | 79 |
3369 | 80 @DOCSTRING(find) |
3294 | 81 |
3428 | 82 @DOCSTRING(common_size) |
83 | |
4167 | 84 @node Rearranging Matrices |
3294 | 85 @section Rearranging Matrices |
86 | |
3369 | 87 @DOCSTRING(fliplr) |
3294 | 88 |
3369 | 89 @DOCSTRING(flipud) |
3294 | 90 |
4869 | 91 @DOCSTRING(flipdim) |
92 | |
3369 | 93 @DOCSTRING(rot90) |
3294 | 94 |
4869 | 95 @DOCSTRING(rotdim) |
96 | |
4845 | 97 @DOCSTRING(cat) |
98 | |
99 @DOCSTRING(horzcat) | |
100 | |
101 @DOCSTRING(vertcat) | |
102 | |
103 @DOCSTRING(permute) | |
104 | |
105 @DOCSTRING(ipermute) | |
106 | |
3369 | 107 @DOCSTRING(reshape) |
3294 | 108 |
8432
c9cb8f0b8b4f
add reference to resize function in the manual
Francesco Potortì <pot@gnu.org>
parents:
8286
diff
changeset
|
109 @DOCSTRING(resize) |
c9cb8f0b8b4f
add reference to resize function in the manual
Francesco Potortì <pot@gnu.org>
parents:
8286
diff
changeset
|
110 |
4894 | 111 @DOCSTRING(circshift) |
112 | |
113 @DOCSTRING(shiftdim) | |
114 | |
3369 | 115 @DOCSTRING(shift) |
3294 | 116 |
3369 | 117 @DOCSTRING(sort) |
3294 | 118 |
6550 | 119 @DOCSTRING(sortrows) |
120 | |
3294 | 121 Since the @code{sort} function does not allow sort keys to be specified, |
122 it can't be used to order the rows of a matrix according to the values | |
123 of the elements in various columns@footnote{For example, to first sort | |
124 based on the values in column 1, and then, for any values that are | |
125 repeated in column 1, sort based on the values found in column 2, etc.} | |
126 in a single call. Using the second output, however, it is possible to | |
127 sort all rows based on the values in a given column. Here's an example | |
128 that sorts the rows of a matrix based on the values in the second | |
129 column. | |
130 | |
131 @example | |
132 @group | |
133 a = [1, 2; 2, 3; 3, 1]; | |
134 [s, i] = sort (a (:, 2)); | |
135 a (i, :) | |
136 @result{} 3 1 | |
137 1 2 | |
138 2 3 | |
139 @end group | |
140 @end example | |
141 | |
6550 | 142 @DOCSTRING(swap) |
143 | |
144 @DOCSTRING(swapcols) | |
145 | |
146 @DOCSTRING(swaprows) | |
147 | |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8133
diff
changeset
|
148 @anchor{doc-triu} |
3369 | 149 @DOCSTRING(tril) |
3294 | 150 |
3369 | 151 @DOCSTRING(vec) |
3294 | 152 |
3369 | 153 @DOCSTRING(vech) |
3294 | 154 |
8512
137d6c94212f
fix missing anchor in matrix.txi
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8432
diff
changeset
|
155 @anchor{doc-postpad} |
3428 | 156 @DOCSTRING(prepad) |
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 | |
3369 | 178 @DOCSTRING(rand) |
3294 | 179 |
3369 | 180 @DOCSTRING(randn) |
3294 | 181 |
5730 | 182 @DOCSTRING(rande) |
183 | |
184 @DOCSTRING(randp) | |
185 | |
186 @DOCSTRING(randg) | |
187 | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
188 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
|
189 possible to mix the two. Initializing any generator with |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
190 @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
|
191 same style for future calls. |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
192 |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
193 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
|
194 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
|
195 example, |
3294 | 196 |
197 @example | |
198 @group | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
199 rand ("state", [11, 22, 33]); |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
200 randn ("state", [44, 55, 66]); |
3294 | 201 u = rand (100, 1); |
202 n = randn (100, 1); | |
203 @end group | |
204 @end example | |
205 | |
206 @noindent | |
207 and | |
208 | |
209 @example | |
210 @group | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
211 rand ("state", [11, 22, 33]); |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
212 randn ("state", [44, 55, 66]); |
3294 | 213 u = zeros (100, 1); |
214 n = zeros (100, 1); | |
215 for i = 1:100 | |
216 u(i) = rand (); | |
217 n(i) = randn (); | |
218 end | |
219 @end group | |
220 @end example | |
221 | |
222 @noindent | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
223 produce equivalent results. When the generators are initialized in |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
224 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
|
225 independent, because the old @code{rande}, @code{randg} and |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
226 @code{randp} generators make calls to @code{rand} and @code{randn}. |
3294 | 227 |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
228 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
|
229 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
|
230 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
|
231 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
|
232 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
|
233 or seed to a specific value. |
3294 | 234 |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
235 If invoked without arguments, @code{rand} and @code{randn} return a |
3294 | 236 single element of a random sequence. |
237 | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
238 The original @code{rand} and @code{randn} functions use Fortran code from |
3294 | 239 @sc{Ranlib}, a library of fortran routines for random number generation, |
240 compiled by Barry W. Brown and James Lovato of the Department of | |
241 Biomathematics at The University of Texas, M.D. Anderson Cancer Center, | |
242 Houston, TX 77030. | |
243 | |
3428 | 244 @DOCSTRING(randperm) |
245 | |
3369 | 246 @DOCSTRING(diag) |
3294 | 247 |
248 The functions @code{linspace} and @code{logspace} make it very easy to | |
249 create vectors with evenly or logarithmically spaced elements. | |
250 @xref{Ranges}. | |
251 | |
3369 | 252 @DOCSTRING(linspace) |
3294 | 253 |
3369 | 254 @DOCSTRING(logspace) |
3294 | 255 |
4167 | 256 @node Famous Matrices |
3294 | 257 @section Famous Matrices |
258 | |
259 The following functions return famous matrix forms. | |
260 | |
6502 | 261 @DOCSTRING(hadamard) |
262 | |
3369 | 263 @DOCSTRING(hankel) |
3294 | 264 |
3369 | 265 @DOCSTRING(hilb) |
3294 | 266 |
3369 | 267 @DOCSTRING(invhilb) |
3294 | 268 |
6502 | 269 @DOCSTRING(magic) |
270 | |
271 @DOCSTRING(pascal) | |
272 | |
273 @DOCSTRING(rosser) | |
274 | |
3369 | 275 @DOCSTRING(sylvester_matrix) |
3294 | 276 |
3369 | 277 @DOCSTRING(toeplitz) |
3294 | 278 |
3369 | 279 @DOCSTRING(vander) |
6502 | 280 |
281 @DOCSTRING(wilkinson) |