Mercurial > hg > octave-nkf
annotate doc/interpreter/matrix.txi @ 8286:6f2d95255911
fix @seealso references to point to existing anchors
author | Thorsten Meyer <thorsten.meyier@gmx.de> |
---|---|
date | Wed, 29 Oct 2008 17:52:54 -0400 |
parents | f38997cf9e5b |
children | c9cb8f0b8b4f |
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 |
4894 | 109 @DOCSTRING(circshift) |
110 | |
111 @DOCSTRING(shiftdim) | |
112 | |
3369 | 113 @DOCSTRING(shift) |
3294 | 114 |
3369 | 115 @DOCSTRING(sort) |
3294 | 116 |
6550 | 117 @DOCSTRING(sortrows) |
118 | |
3294 | 119 Since the @code{sort} function does not allow sort keys to be specified, |
120 it can't be used to order the rows of a matrix according to the values | |
121 of the elements in various columns@footnote{For example, to first sort | |
122 based on the values in column 1, and then, for any values that are | |
123 repeated in column 1, sort based on the values found in column 2, etc.} | |
124 in a single call. Using the second output, however, it is possible to | |
125 sort all rows based on the values in a given column. Here's an example | |
126 that sorts the rows of a matrix based on the values in the second | |
127 column. | |
128 | |
129 @example | |
130 @group | |
131 a = [1, 2; 2, 3; 3, 1]; | |
132 [s, i] = sort (a (:, 2)); | |
133 a (i, :) | |
134 @result{} 3 1 | |
135 1 2 | |
136 2 3 | |
137 @end group | |
138 @end example | |
139 | |
6550 | 140 @DOCSTRING(swap) |
141 | |
142 @DOCSTRING(swapcols) | |
143 | |
144 @DOCSTRING(swaprows) | |
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 |
3428 | 153 @DOCSTRING(prepad) |
154 | |
6550 | 155 @DOCSTRING(blkdiag) |
156 | |
157 @node Applying a Function to an Array | |
158 @section Applying a Function to an Array | |
159 | |
160 @DOCSTRING(arrayfun) | |
161 | |
6868 | 162 @DOCSTRING(bsxfun) |
163 | |
4167 | 164 @node Special Utility Matrices |
3294 | 165 @section Special Utility Matrices |
166 | |
3369 | 167 @DOCSTRING(eye) |
3294 | 168 |
3369 | 169 @DOCSTRING(ones) |
3294 | 170 |
3369 | 171 @DOCSTRING(zeros) |
3294 | 172 |
3920 | 173 @DOCSTRING(repmat) |
174 | |
3369 | 175 @DOCSTRING(rand) |
3294 | 176 |
3369 | 177 @DOCSTRING(randn) |
3294 | 178 |
5730 | 179 @DOCSTRING(rande) |
180 | |
181 @DOCSTRING(randp) | |
182 | |
183 @DOCSTRING(randg) | |
184 | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
185 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
|
186 possible to mix the two. Initializing any generator with |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
187 @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
|
188 same style for future calls. |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
189 |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
190 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
|
191 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
|
192 example, |
3294 | 193 |
194 @example | |
195 @group | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
196 rand ("state", [11, 22, 33]); |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
197 randn ("state", [44, 55, 66]); |
3294 | 198 u = rand (100, 1); |
199 n = randn (100, 1); | |
200 @end group | |
201 @end example | |
202 | |
203 @noindent | |
204 and | |
205 | |
206 @example | |
207 @group | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
208 rand ("state", [11, 22, 33]); |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
209 randn ("state", [44, 55, 66]); |
3294 | 210 u = zeros (100, 1); |
211 n = zeros (100, 1); | |
212 for i = 1:100 | |
213 u(i) = rand (); | |
214 n(i) = randn (); | |
215 end | |
216 @end group | |
217 @end example | |
218 | |
219 @noindent | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
220 produce equivalent results. When the generators are initialized in |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
221 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
|
222 independent, because the old @code{rande}, @code{randg} and |
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
223 @code{randp} generators make calls to @code{rand} and @code{randn}. |
3294 | 224 |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
225 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
|
226 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
|
227 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
|
228 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
|
229 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
|
230 or seed to a specific value. |
3294 | 231 |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
232 If invoked without arguments, @code{rand} and @code{randn} return a |
3294 | 233 single element of a random sequence. |
234 | |
8133
f38997cf9e5b
matrix.txi: update docs for random number generators
Brian Gough
parents:
7018
diff
changeset
|
235 The original @code{rand} and @code{randn} functions use Fortran code from |
3294 | 236 @sc{Ranlib}, a library of fortran routines for random number generation, |
237 compiled by Barry W. Brown and James Lovato of the Department of | |
238 Biomathematics at The University of Texas, M.D. Anderson Cancer Center, | |
239 Houston, TX 77030. | |
240 | |
3428 | 241 @DOCSTRING(randperm) |
242 | |
3369 | 243 @DOCSTRING(diag) |
3294 | 244 |
245 The functions @code{linspace} and @code{logspace} make it very easy to | |
246 create vectors with evenly or logarithmically spaced elements. | |
247 @xref{Ranges}. | |
248 | |
3369 | 249 @DOCSTRING(linspace) |
3294 | 250 |
3369 | 251 @DOCSTRING(logspace) |
3294 | 252 |
4167 | 253 @node Famous Matrices |
3294 | 254 @section Famous Matrices |
255 | |
256 The following functions return famous matrix forms. | |
257 | |
6502 | 258 @DOCSTRING(hadamard) |
259 | |
3369 | 260 @DOCSTRING(hankel) |
3294 | 261 |
3369 | 262 @DOCSTRING(hilb) |
3294 | 263 |
3369 | 264 @DOCSTRING(invhilb) |
3294 | 265 |
6502 | 266 @DOCSTRING(magic) |
267 | |
268 @DOCSTRING(pascal) | |
269 | |
270 @DOCSTRING(rosser) | |
271 | |
3369 | 272 @DOCSTRING(sylvester_matrix) |
3294 | 273 |
3369 | 274 @DOCSTRING(toeplitz) |
3294 | 275 |
3369 | 276 @DOCSTRING(vander) |
6502 | 277 |
278 @DOCSTRING(wilkinson) |