6778
|
1 @c Copyright (C) 1996, 1997, 2007 John W. Eaton |
3294
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
4167
|
5 @node Matrix Manipulation |
3294
|
6 @chapter Matrix Manipulation |
|
7 |
|
8 There are a number of functions available for checking to see if the |
|
9 elements of a matrix meet some condition, and for rearranging the |
|
10 elements of a matrix. For example, Octave can easily tell you if all |
|
11 the elements of a matrix are finite, or are less than some specified |
|
12 value. Octave can also rotate the elements, extract the upper- or |
|
13 lower-triangular parts, or sort the columns of a matrix. |
|
14 |
|
15 @menu |
|
16 * Finding Elements and Checking Conditions:: |
|
17 * Rearranging Matrices:: |
6550
|
18 * Applying a Function to an Array:: |
3294
|
19 * Special Utility Matrices:: |
|
20 * Famous Matrices:: |
|
21 @end menu |
|
22 |
4167
|
23 @node Finding Elements and Checking Conditions |
3294
|
24 @section Finding Elements and Checking Conditions |
|
25 |
|
26 The functions @code{any} and @code{all} are useful for determining |
|
27 whether any or all of the elements of a matrix satisfy some condition. |
|
28 The @code{find} function is also useful in determining which elements of |
|
29 a matrix meet a specified condition. |
|
30 |
3369
|
31 @DOCSTRING(any) |
3294
|
32 |
3369
|
33 @DOCSTRING(all) |
3294
|
34 |
|
35 Since the comparison operators (@pxref{Comparison Ops}) return matrices |
|
36 of ones and zeros, it is easy to test a matrix for many things, not just |
|
37 whether the elements are nonzero. For example, |
|
38 |
|
39 @example |
|
40 @group |
|
41 all (all (rand (5) < 0.9)) |
|
42 @result{} 0 |
|
43 @end group |
|
44 @end example |
|
45 |
|
46 @noindent |
|
47 tests a random 5 by 5 matrix to see if all of its elements are less |
|
48 than 0.9. |
|
49 |
|
50 Note that in conditional contexts (like the test clause of @code{if} and |
|
51 @code{while} statements) Octave treats the test as if you had typed |
|
52 @code{all (all (condition))}. |
|
53 |
3428
|
54 @DOCSTRING(xor) |
|
55 |
|
56 @DOCSTRING(is_duplicate_entry) |
3294
|
57 |
3369
|
58 @DOCSTRING(diff) |
3294
|
59 |
3369
|
60 @DOCSTRING(isinf) |
3294
|
61 |
3369
|
62 @DOCSTRING(isnan) |
3294
|
63 |
3369
|
64 @DOCSTRING(finite) |
3294
|
65 |
3369
|
66 @DOCSTRING(find) |
3294
|
67 |
3428
|
68 @DOCSTRING(common_size) |
|
69 |
4167
|
70 @node Rearranging Matrices |
3294
|
71 @section Rearranging Matrices |
|
72 |
3369
|
73 @DOCSTRING(fliplr) |
3294
|
74 |
3369
|
75 @DOCSTRING(flipud) |
3294
|
76 |
4869
|
77 @DOCSTRING(flipdim) |
|
78 |
3369
|
79 @DOCSTRING(rot90) |
3294
|
80 |
4869
|
81 @DOCSTRING(rotdim) |
|
82 |
4845
|
83 @DOCSTRING(cat) |
|
84 |
|
85 @DOCSTRING(horzcat) |
|
86 |
|
87 @DOCSTRING(vertcat) |
|
88 |
|
89 @DOCSTRING(permute) |
|
90 |
|
91 @DOCSTRING(ipermute) |
|
92 |
3369
|
93 @DOCSTRING(reshape) |
3294
|
94 |
4894
|
95 @DOCSTRING(circshift) |
|
96 |
|
97 @DOCSTRING(shiftdim) |
|
98 |
3369
|
99 @DOCSTRING(shift) |
3294
|
100 |
3369
|
101 @DOCSTRING(sort) |
3294
|
102 |
6550
|
103 @DOCSTRING(sortrows) |
|
104 |
3294
|
105 Since the @code{sort} function does not allow sort keys to be specified, |
|
106 it can't be used to order the rows of a matrix according to the values |
|
107 of the elements in various columns@footnote{For example, to first sort |
|
108 based on the values in column 1, and then, for any values that are |
|
109 repeated in column 1, sort based on the values found in column 2, etc.} |
|
110 in a single call. Using the second output, however, it is possible to |
|
111 sort all rows based on the values in a given column. Here's an example |
|
112 that sorts the rows of a matrix based on the values in the second |
|
113 column. |
|
114 |
|
115 @example |
|
116 @group |
|
117 a = [1, 2; 2, 3; 3, 1]; |
|
118 [s, i] = sort (a (:, 2)); |
|
119 a (i, :) |
|
120 @result{} 3 1 |
|
121 1 2 |
|
122 2 3 |
|
123 @end group |
|
124 @end example |
|
125 |
6550
|
126 @DOCSTRING(swap) |
|
127 |
|
128 @DOCSTRING(swapcols) |
|
129 |
|
130 @DOCSTRING(swaprows) |
|
131 |
3369
|
132 @DOCSTRING(tril) |
3294
|
133 |
3369
|
134 @DOCSTRING(vec) |
3294
|
135 |
3369
|
136 @DOCSTRING(vech) |
3294
|
137 |
3428
|
138 @DOCSTRING(prepad) |
|
139 |
6550
|
140 @DOCSTRING(blkdiag) |
|
141 |
|
142 @node Applying a Function to an Array |
|
143 @section Applying a Function to an Array |
|
144 |
|
145 @DOCSTRING(arrayfun) |
|
146 |
4167
|
147 @node Special Utility Matrices |
3294
|
148 @section Special Utility Matrices |
|
149 |
3369
|
150 @DOCSTRING(eye) |
3294
|
151 |
3369
|
152 @DOCSTRING(ones) |
3294
|
153 |
3369
|
154 @DOCSTRING(zeros) |
3294
|
155 |
3920
|
156 @DOCSTRING(repmat) |
|
157 |
3369
|
158 @DOCSTRING(rand) |
3294
|
159 |
3369
|
160 @DOCSTRING(randn) |
3294
|
161 |
5730
|
162 @DOCSTRING(rande) |
|
163 |
|
164 @DOCSTRING(randp) |
|
165 |
|
166 @DOCSTRING(randg) |
|
167 |
|
168 The new random generators all use a common Mersenne Twister generator, |
|
169 and so the state of only one of the generators needs to be reset. |
|
170 The old generator function use separate generators. This ensures that |
3294
|
171 |
|
172 @example |
|
173 @group |
|
174 rand ("seed", 13); |
|
175 randn ("seed", 13); |
|
176 u = rand (100, 1); |
|
177 n = randn (100, 1); |
|
178 @end group |
|
179 @end example |
|
180 |
|
181 @noindent |
|
182 and |
|
183 |
|
184 @example |
|
185 @group |
|
186 rand ("seed", 13); |
|
187 randn ("seed", 13); |
|
188 u = zeros (100, 1); |
|
189 n = zeros (100, 1); |
|
190 for i = 1:100 |
|
191 u(i) = rand (); |
|
192 n(i) = randn (); |
|
193 end |
|
194 @end group |
|
195 @end example |
|
196 |
|
197 @noindent |
|
198 produce equivalent results. |
|
199 |
|
200 Normally, @code{rand} and @code{randn} obtain their initial |
|
201 seeds from the system clock, so that the sequence of random numbers is |
|
202 not the same each time you run Octave. If you really do need for to |
|
203 reproduce a sequence of numbers exactly, you can set the seed to a |
|
204 specific value. |
|
205 |
|
206 If it is invoked without arguments, @code{rand} and @code{randn} return a |
|
207 single element of a random sequence. |
|
208 |
|
209 The @code{rand} and @code{randn} functions use Fortran code from |
|
210 @sc{Ranlib}, a library of fortran routines for random number generation, |
|
211 compiled by Barry W. Brown and James Lovato of the Department of |
|
212 Biomathematics at The University of Texas, M.D. Anderson Cancer Center, |
|
213 Houston, TX 77030. |
|
214 |
3428
|
215 @DOCSTRING(randperm) |
|
216 |
3369
|
217 @DOCSTRING(diag) |
3294
|
218 |
|
219 The functions @code{linspace} and @code{logspace} make it very easy to |
|
220 create vectors with evenly or logarithmically spaced elements. |
|
221 @xref{Ranges}. |
|
222 |
3369
|
223 @DOCSTRING(linspace) |
3294
|
224 |
3369
|
225 @DOCSTRING(logspace) |
3294
|
226 |
4167
|
227 @node Famous Matrices |
3294
|
228 @section Famous Matrices |
|
229 |
|
230 The following functions return famous matrix forms. |
|
231 |
6502
|
232 @DOCSTRING(hadamard) |
|
233 |
3369
|
234 @DOCSTRING(hankel) |
3294
|
235 |
3369
|
236 @DOCSTRING(hilb) |
3294
|
237 |
3369
|
238 @DOCSTRING(invhilb) |
3294
|
239 |
6502
|
240 @DOCSTRING(magic) |
|
241 |
|
242 @DOCSTRING(pascal) |
|
243 |
|
244 @DOCSTRING(rosser) |
|
245 |
3369
|
246 @DOCSTRING(sylvester_matrix) |
3294
|
247 |
3369
|
248 @DOCSTRING(toeplitz) |
3294
|
249 |
3369
|
250 @DOCSTRING(vander) |
6502
|
251 |
|
252 @DOCSTRING(wilkinson) |