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 |
3369
|
94 @DOCSTRING(shift) |
3294
|
95 |
3369
|
96 @DOCSTRING(sort) |
3294
|
97 |
|
98 Since the @code{sort} function does not allow sort keys to be specified, |
|
99 it can't be used to order the rows of a matrix according to the values |
|
100 of the elements in various columns@footnote{For example, to first sort |
|
101 based on the values in column 1, and then, for any values that are |
|
102 repeated in column 1, sort based on the values found in column 2, etc.} |
|
103 in a single call. Using the second output, however, it is possible to |
|
104 sort all rows based on the values in a given column. Here's an example |
|
105 that sorts the rows of a matrix based on the values in the second |
|
106 column. |
|
107 |
|
108 @example |
|
109 @group |
|
110 a = [1, 2; 2, 3; 3, 1]; |
|
111 [s, i] = sort (a (:, 2)); |
|
112 a (i, :) |
|
113 @result{} 3 1 |
|
114 1 2 |
|
115 2 3 |
|
116 @end group |
|
117 @end example |
|
118 |
3369
|
119 @DOCSTRING(tril) |
3294
|
120 |
3369
|
121 @DOCSTRING(vec) |
3294
|
122 |
3369
|
123 @DOCSTRING(vech) |
3294
|
124 |
3428
|
125 @DOCSTRING(prepad) |
|
126 |
4167
|
127 @node Special Utility Matrices |
3294
|
128 @section Special Utility Matrices |
|
129 |
3369
|
130 @DOCSTRING(eye) |
3294
|
131 |
3369
|
132 @DOCSTRING(ones) |
3294
|
133 |
3369
|
134 @DOCSTRING(zeros) |
3294
|
135 |
3920
|
136 @DOCSTRING(repmat) |
|
137 |
3369
|
138 @DOCSTRING(rand) |
3294
|
139 |
3369
|
140 @DOCSTRING(randn) |
3294
|
141 |
|
142 The @code{rand} and @code{randn} functions use separate generators. |
|
143 This ensures that |
|
144 |
|
145 @example |
|
146 @group |
|
147 rand ("seed", 13); |
|
148 randn ("seed", 13); |
|
149 u = rand (100, 1); |
|
150 n = randn (100, 1); |
|
151 @end group |
|
152 @end example |
|
153 |
|
154 @noindent |
|
155 and |
|
156 |
|
157 @example |
|
158 @group |
|
159 rand ("seed", 13); |
|
160 randn ("seed", 13); |
|
161 u = zeros (100, 1); |
|
162 n = zeros (100, 1); |
|
163 for i = 1:100 |
|
164 u(i) = rand (); |
|
165 n(i) = randn (); |
|
166 end |
|
167 @end group |
|
168 @end example |
|
169 |
|
170 @noindent |
|
171 produce equivalent results. |
|
172 |
|
173 Normally, @code{rand} and @code{randn} obtain their initial |
|
174 seeds from the system clock, so that the sequence of random numbers is |
|
175 not the same each time you run Octave. If you really do need for to |
|
176 reproduce a sequence of numbers exactly, you can set the seed to a |
|
177 specific value. |
|
178 |
|
179 If it is invoked without arguments, @code{rand} and @code{randn} return a |
|
180 single element of a random sequence. |
|
181 |
|
182 The @code{rand} and @code{randn} functions use Fortran code from |
|
183 @sc{Ranlib}, a library of fortran routines for random number generation, |
|
184 compiled by Barry W. Brown and James Lovato of the Department of |
|
185 Biomathematics at The University of Texas, M.D. Anderson Cancer Center, |
|
186 Houston, TX 77030. |
|
187 |
3428
|
188 @DOCSTRING(randperm) |
|
189 |
3369
|
190 @DOCSTRING(diag) |
3294
|
191 |
|
192 @c XXX FIXME XXX -- is this really worth documenting? |
|
193 @c |
4451
|
194 @c DOCSTRING(warn_imag_to_real) |
3294
|
195 @c |
|
196 @c XXX FIXME XXX -- this is here because it is used by @code{ones}, |
|
197 @c @code{zeros}, @code{rand}, etc. |
|
198 |
|
199 The functions @code{linspace} and @code{logspace} make it very easy to |
|
200 create vectors with evenly or logarithmically spaced elements. |
|
201 @xref{Ranges}. |
|
202 |
3369
|
203 @DOCSTRING(linspace) |
3294
|
204 |
3369
|
205 @DOCSTRING(logspace) |
3294
|
206 |
4457
|
207 @DOCSTRING(warn_neg_dim_as_zero) |
3294
|
208 |
4451
|
209 @DOCSTRING(warn_imag_to_real) |
3428
|
210 |
4167
|
211 @node Famous Matrices |
3294
|
212 @section Famous Matrices |
|
213 |
|
214 The following functions return famous matrix forms. |
|
215 |
3369
|
216 @DOCSTRING(hankel) |
3294
|
217 |
3369
|
218 @DOCSTRING(hilb) |
3294
|
219 |
3369
|
220 @DOCSTRING(invhilb) |
3294
|
221 |
3369
|
222 @DOCSTRING(sylvester_matrix) |
3294
|
223 |
3369
|
224 @DOCSTRING(toeplitz) |
3294
|
225 |
3369
|
226 @DOCSTRING(vander) |