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