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 |
3369
|
146 @DOCSTRING(tril) |
3294
|
147 |
3369
|
148 @DOCSTRING(vec) |
3294
|
149 |
3369
|
150 @DOCSTRING(vech) |
3294
|
151 |
3428
|
152 @DOCSTRING(prepad) |
|
153 |
6550
|
154 @DOCSTRING(blkdiag) |
|
155 |
|
156 @node Applying a Function to an Array |
|
157 @section Applying a Function to an Array |
|
158 |
|
159 @DOCSTRING(arrayfun) |
|
160 |
6868
|
161 @DOCSTRING(bsxfun) |
|
162 |
4167
|
163 @node Special Utility Matrices |
3294
|
164 @section Special Utility Matrices |
|
165 |
3369
|
166 @DOCSTRING(eye) |
3294
|
167 |
3369
|
168 @DOCSTRING(ones) |
3294
|
169 |
3369
|
170 @DOCSTRING(zeros) |
3294
|
171 |
3920
|
172 @DOCSTRING(repmat) |
|
173 |
3369
|
174 @DOCSTRING(rand) |
3294
|
175 |
3369
|
176 @DOCSTRING(randn) |
3294
|
177 |
5730
|
178 @DOCSTRING(rande) |
|
179 |
|
180 @DOCSTRING(randp) |
|
181 |
|
182 @DOCSTRING(randg) |
|
183 |
|
184 The new random generators all use a common Mersenne Twister generator, |
|
185 and so the state of only one of the generators needs to be reset. |
|
186 The old generator function use separate generators. This ensures that |
3294
|
187 |
|
188 @example |
|
189 @group |
|
190 rand ("seed", 13); |
|
191 randn ("seed", 13); |
|
192 u = rand (100, 1); |
|
193 n = randn (100, 1); |
|
194 @end group |
|
195 @end example |
|
196 |
|
197 @noindent |
|
198 and |
|
199 |
|
200 @example |
|
201 @group |
|
202 rand ("seed", 13); |
|
203 randn ("seed", 13); |
|
204 u = zeros (100, 1); |
|
205 n = zeros (100, 1); |
|
206 for i = 1:100 |
|
207 u(i) = rand (); |
|
208 n(i) = randn (); |
|
209 end |
|
210 @end group |
|
211 @end example |
|
212 |
|
213 @noindent |
|
214 produce equivalent results. |
|
215 |
|
216 Normally, @code{rand} and @code{randn} obtain their initial |
|
217 seeds from the system clock, so that the sequence of random numbers is |
|
218 not the same each time you run Octave. If you really do need for to |
|
219 reproduce a sequence of numbers exactly, you can set the seed to a |
|
220 specific value. |
|
221 |
|
222 If it is invoked without arguments, @code{rand} and @code{randn} return a |
|
223 single element of a random sequence. |
|
224 |
|
225 The @code{rand} and @code{randn} functions use Fortran code from |
|
226 @sc{Ranlib}, a library of fortran routines for random number generation, |
|
227 compiled by Barry W. Brown and James Lovato of the Department of |
|
228 Biomathematics at The University of Texas, M.D. Anderson Cancer Center, |
|
229 Houston, TX 77030. |
|
230 |
3428
|
231 @DOCSTRING(randperm) |
|
232 |
3369
|
233 @DOCSTRING(diag) |
3294
|
234 |
|
235 The functions @code{linspace} and @code{logspace} make it very easy to |
|
236 create vectors with evenly or logarithmically spaced elements. |
|
237 @xref{Ranges}. |
|
238 |
3369
|
239 @DOCSTRING(linspace) |
3294
|
240 |
3369
|
241 @DOCSTRING(logspace) |
3294
|
242 |
4167
|
243 @node Famous Matrices |
3294
|
244 @section Famous Matrices |
|
245 |
|
246 The following functions return famous matrix forms. |
|
247 |
6502
|
248 @DOCSTRING(hadamard) |
|
249 |
3369
|
250 @DOCSTRING(hankel) |
3294
|
251 |
3369
|
252 @DOCSTRING(hilb) |
3294
|
253 |
3369
|
254 @DOCSTRING(invhilb) |
3294
|
255 |
6502
|
256 @DOCSTRING(magic) |
|
257 |
|
258 @DOCSTRING(pascal) |
|
259 |
|
260 @DOCSTRING(rosser) |
|
261 |
3369
|
262 @DOCSTRING(sylvester_matrix) |
3294
|
263 |
3369
|
264 @DOCSTRING(toeplitz) |
3294
|
265 |
3369
|
266 @DOCSTRING(vander) |
6502
|
267 |
|
268 @DOCSTRING(wilkinson) |