2333
|
1 @c Copyright (C) 1996 John W. Eaton |
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
|
5 @node Special Matrices, Matrix Manipulation, Input and Output, Top |
|
6 @chapter Special Matrices |
|
7 |
|
8 Octave provides a number of functions for creating special matrix forms. |
|
9 In nearly all cases, it is best to use the built-in functions for this |
|
10 purpose than to try to use other tricks to achieve the same effect. |
|
11 |
|
12 @menu |
|
13 * Special Utility Matrices:: |
|
14 * Famous Matrices:: |
|
15 @end menu |
|
16 |
|
17 @node Special Utility Matrices, Famous Matrices, Special Matrices, Special Matrices |
|
18 @section Special Utility Matrices |
|
19 |
2449
|
20 @deftypefn {Built-in Function} {} eye (@var{x}) |
|
21 @deftypefnx {Built-in Function} {} eye (@var{n}, @var{m}) |
|
22 Returns an identity matrix. If invoked with a single scalar argument, |
|
23 @code{eye} returns a square matrix with the dimension specified. If you |
|
24 supply two scalar arguments, @code{eye} takes them to be the number of |
|
25 rows and columns. If given a vector with two elements, @code{eye} uses |
|
26 the values of the elements as the number of rows and columns, |
|
27 respecively. For example, |
2333
|
28 |
|
29 @example |
2449
|
30 @group |
2333
|
31 eye (3) |
|
32 |
2449
|
33 @result{} 1 0 0 |
|
34 0 1 0 |
|
35 0 0 1 |
|
36 @end group |
2333
|
37 @end example |
|
38 |
2449
|
39 The following expressions all produce the same result: |
2333
|
40 |
|
41 @example |
2449
|
42 @group |
|
43 eye (2) |
|
44 eye (2, 2) |
|
45 eye (size ([1, 2; 3, 4]) |
|
46 @end group |
2333
|
47 @end example |
|
48 |
2449
|
49 For compatibility with @sc{Matlab}, calling @code{eye} with no arguments |
|
50 is equivalent to calling it with an argument of 1. |
|
51 @end deftypefn |
2333
|
52 |
2449
|
53 @deftypefn {Built-in Function} {} ones (@var{x}) |
|
54 @deftypefnx {Built-in Function} {} ones (@var{n}, @var{m}) |
|
55 Returns a matrix whose elements are all 1. The arguments are handled |
|
56 the same as the arguments for @code{eye}. |
2333
|
57 |
|
58 If you need to create a matrix whose values are all the same, you should |
|
59 use an expression like |
|
60 |
|
61 @example |
|
62 val_matrix = val * ones (n, m) |
|
63 @end example |
2449
|
64 @end deftypefn |
2333
|
65 |
2449
|
66 @deftypefn {Built-in Function} {} zeros (@var{x}) |
|
67 @deftypefnx {Built-in Function} {} zeros (@var{n}, @var{m}) |
|
68 Returns a matrix whose elements are all 0. The arguments are handled |
|
69 the same as the arguments for @code{eye}. |
|
70 @end deftypefn |
|
71 |
|
72 @deftypefn {Built-in Function} {} rand (@var{x}) |
|
73 @deftypefnx {Built-in Function} {} rand (@var{n}, @var{m}) |
|
74 @deftypefnx {Built-in Function} {} rand (@code{"seed"}, @var{x}) |
|
75 Returns a matrix with random elements uniformly distributed on the |
|
76 interval (0, 1). The arguments are handled the same as the arguments |
|
77 for @code{eye}. In |
|
78 addition, you can set the seed for the random number generator using the |
|
79 form |
2333
|
80 |
|
81 @example |
2449
|
82 randn ("seed", @var{x}) |
2333
|
83 @end example |
|
84 |
|
85 @noindent |
2449
|
86 where @var{x} is a scalar value. If called as |
2333
|
87 |
|
88 @example |
2449
|
89 rand ("seed") |
|
90 @end example |
|
91 |
|
92 @noindent |
|
93 @code{rand} returns the current value of the seed. |
|
94 @end deftypefn |
|
95 |
|
96 @deftypefn {Built-in Function} {} randn (@var{x}) |
|
97 @deftypefnx {Built-in Function} {} randn (@var{n}, @var{m}) |
|
98 @deftypefnx {Built-in Function} {} randn (@code{"seed"}, @var{x}) |
|
99 Returns a matrix with normally distributed random elements. The |
|
100 arguments are handled the same as the arguments for @code{eye}. In |
|
101 addition, you can set the seed for the random number generator using the |
|
102 form |
|
103 |
|
104 @example |
|
105 randn ("seed", @var{x}) |
|
106 @end example |
|
107 |
|
108 @noindent |
|
109 where @var{x} is a scalar value. If called as |
|
110 |
|
111 @example |
|
112 randn ("seed") |
2333
|
113 @end example |
|
114 |
|
115 @noindent |
2449
|
116 @code{randn} returns the current value of the seed. |
|
117 @end deftypefn |
2333
|
118 |
2449
|
119 The @code{rand} and @code{randn} functions use separate generators. |
|
120 This ensures that |
2333
|
121 |
|
122 @example |
2449
|
123 @group |
|
124 rand ("seed", 13); |
|
125 randn ("seed", 13); |
|
126 u = rand (100, 1); |
|
127 n = randn (100, 1); |
|
128 @end group |
2333
|
129 @end example |
|
130 |
2449
|
131 @noindent |
|
132 and |
2333
|
133 |
|
134 @example |
2449
|
135 @group |
|
136 rand ("seed", 13); |
|
137 randn ("seed", 13); |
|
138 u = zeros (100, 1); |
|
139 n = zeros (100, 1); |
|
140 for i = 1:100 |
|
141 u(i) = rand (); |
|
142 n(i) = randn (); |
|
143 end |
|
144 @end group |
2333
|
145 @end example |
|
146 |
|
147 @noindent |
2449
|
148 produce equivalent results. |
|
149 |
|
150 Normally, @code{rand} and @code{randn} obtain their initial |
|
151 seeds from the system clock, so that the sequence of random numbers is |
|
152 not the same each time you run Octave. If you really do need for to |
|
153 reproduce a sequence of numbers exactly, you can set the seed to a |
|
154 specific value. |
|
155 |
|
156 If it is invoked without arguments, @code{rand} and @code{randn} return a |
|
157 single element of a random sequence. |
|
158 |
|
159 The @code{rand} and @code{randn} functions use Fortran code from RANLIB, |
|
160 a library of fortran routines for random number generation, compiled by |
|
161 Barry W. Brown and James Lovato of the Department of Biomathematics at |
|
162 The University of Texas, M.D. Anderson Cancer Center, Houston, TX 77030. |
|
163 |
|
164 @deftypefn {Built-in Function} {} diag (@var{v}, @var{k}) |
|
165 Returns a diagonal matrix with vector @var{v} on diagonal @var{k}. The |
|
166 second argument is optional. If it is positive, the vector is placed on |
|
167 the @var{k}-th super-diagonal. If it is negative, it is placed on the |
|
168 @var{-k}-th sub-diagonal. The default value of @var{k} is 0, and the |
|
169 vector is placed on the main diagonal. For example, |
|
170 |
|
171 @example |
|
172 @group |
|
173 diag ([1, 2, 3], 1) |
|
174 |
|
175 @result{} 0 1 0 0 |
|
176 0 0 2 0 |
|
177 0 0 0 3 |
|
178 0 0 0 0 |
|
179 @end group |
|
180 @end example |
|
181 @end deftypefn |
|
182 |
|
183 The functions @code{linspace} and @code{logspace} make it very easy to |
|
184 create vectors with evenly or logarithmically spaced elements. |
|
185 @xref{Ranges}. |
|
186 |
|
187 @deftypefn {Function File} {} linspace (@var{base}, @var{limit}, @var{n}) |
|
188 creates a row vector with @var{n} (@var{n} greater than 1) linearly |
2333
|
189 spaced elements between @var{base} and @var{limit}. The @var{base} and |
|
190 @var{limit} are always included in the range. If @var{base} is greater |
|
191 than @var{limit}, the elements are stored in decreasing order. If the |
|
192 number of points is not specified, a value of 100 is used. |
|
193 |
2449
|
194 The @code{linspace} function always returns a row vector, regardless of |
|
195 the value of @code{prefer_column_vectors}. |
|
196 @end deftypefn |
|
197 |
|
198 @deftypefn {Function File} {} logspace (@var{base}, @var{limit}, @var{n}) |
|
199 Similar to @code{linspace} except that the values are logarithmically |
|
200 spaced. |
2333
|
201 |
|
202 If @var{limit} is equal to |
|
203 @iftex |
|
204 @tex |
|
205 $\pi$, |
|
206 @end tex |
|
207 @end iftex |
|
208 @ifinfo |
|
209 pi, |
|
210 @end ifinfo |
|
211 the points are between |
|
212 @iftex |
|
213 @tex |
|
214 $10^{base}$ and $\pi$, |
|
215 @end tex |
|
216 @end iftex |
|
217 @ifinfo |
|
218 10^base and pi, |
|
219 @end ifinfo |
|
220 @emph{not} |
|
221 @iftex |
|
222 @tex |
|
223 $10^{base}$ and $10^{\pi}$, |
|
224 @end tex |
|
225 @end iftex |
|
226 @ifinfo |
|
227 10^base and 10^pi, |
|
228 @end ifinfo |
|
229 in order to be compatible with the corresponding @sc{Matlab} function. |
2449
|
230 @end deftypefn |
2333
|
231 |
|
232 @node Famous Matrices, , Special Utility Matrices, Special Matrices |
|
233 @section Famous Matrices |
|
234 |
|
235 The following functions return famous matrix forms. |
|
236 |
2449
|
237 @deftypefn {Function File} {} hadamard (@var{k}) |
2333
|
238 Return the Hadamard matrix of order n = 2^k. |
2449
|
239 @end deftypefn |
2333
|
240 |
2449
|
241 @deftypefn {Function File} {} hankel (@var{c}, @var{r}) |
2333
|
242 Return the Hankel matrix constructed given the first column @var{c}, and |
|
243 (optionally) the last row @var{r}. If the last element of @var{c} is |
|
244 not the same as the first element of @var{r}, the last element of |
|
245 @var{c} is used. If the second argument is omitted, the last row is |
|
246 taken to be the same as the first column. |
|
247 |
|
248 A Hankel matrix formed from an m-vector @var{c}, and an n-vector |
|
249 @var{r}, has the elements |
|
250 @iftex |
|
251 @tex |
|
252 $$ |
|
253 H (i, j) = \cases{c_{i+j-1},&$i+j-1\le m$;\cr r_{i+j-m},&otherwise.\cr} |
|
254 $$ |
|
255 @end tex |
|
256 @end iftex |
|
257 @ifinfo |
|
258 |
|
259 @example |
|
260 @group |
|
261 H (i, j) = c (i+j-1), i+j-1 <= m; |
|
262 H (i, j) = r (i+j-m), otherwise |
|
263 @end group |
|
264 @end example |
2449
|
265 @end ifinfo |
|
266 @end deftypefn |
2333
|
267 |
2449
|
268 @deftypefn {Function File} {} hilb (@var{n}) |
2333
|
269 Return the Hilbert matrix of order @var{n}. The |
|
270 @iftex |
|
271 @tex |
|
272 $i,\,j$ |
|
273 @end tex |
|
274 @end iftex |
|
275 @ifinfo |
|
276 i, j |
|
277 @end ifinfo |
|
278 element of a Hilbert matrix is defined as |
|
279 @iftex |
|
280 @tex |
|
281 $$ |
|
282 H (i, j) = {1 \over (i + j - 1)} |
|
283 $$ |
|
284 @end tex |
|
285 @end iftex |
|
286 @ifinfo |
|
287 |
|
288 @example |
|
289 H (i, j) = 1 / (i + j - 1) |
|
290 @end example |
|
291 @end ifinfo |
2449
|
292 @end deftypefn |
2333
|
293 |
2449
|
294 @deftypefn {Function File} {} invhilb (@var{n}) |
2333
|
295 Return the inverse of a Hilbert matrix of order @var{n}. This is exact. |
|
296 Compare with the numerical calculation of @code{inverse (hilb (n))}, |
|
297 which suffers from the ill-conditioning of the Hilbert matrix, and the |
|
298 finite precision of your computer's floating point arithmetic. |
2449
|
299 @end deftypefn |
2333
|
300 |
2449
|
301 @deftypefn {Function File} {} toeplitz (@var{c}, @var{r}) |
2333
|
302 Return the Toeplitz matrix constructed given the first column @var{c}, |
|
303 and (optionally) the first row @var{r}. If the first element of @var{c} |
|
304 is not the same as the first element of @var{r}, the first element of |
|
305 @var{c} is used. If the second argument is omitted, the first row is |
|
306 taken to be the same as the first column. |
|
307 |
|
308 A square Toeplitz matrix has the form |
|
309 @iftex |
|
310 @tex |
|
311 $$ |
|
312 \left[\matrix{c_0 & r_1 & r_2 & \ldots & r_n\cr |
|
313 c_1 & c_0 & r_1 & & c_{n-1}\cr |
|
314 c_2 & c_1 & c_0 & & c_{n-2}\cr |
|
315 \vdots & & & & \vdots\cr |
|
316 c_n & c_{n-1} & c_{n-2} & \ldots & c_0}\right]. |
|
317 $$ |
|
318 @end tex |
|
319 @end iftex |
|
320 @ifinfo |
|
321 |
|
322 @example |
|
323 @group |
|
324 c(0) r(1) r(2) ... r(n) |
|
325 c(1) c(0) r(1) r(n-1) |
|
326 c(2) c(1) c(0) r(n-2) |
|
327 . . |
|
328 . . |
|
329 . . |
|
330 |
|
331 c(n) c(n-1) c(n-2) ... c(0) |
|
332 @end group |
|
333 @end example |
|
334 @end ifinfo |
2449
|
335 @end deftypefn |
2333
|
336 |
2449
|
337 @deftypefn {Function File} {} vander (@var{c}) |
2333
|
338 Return the Vandermonde matrix whose next to last column is @var{c}. |
|
339 |
|
340 A Vandermonde matrix has the form |
|
341 @iftex |
|
342 @tex |
|
343 $$ |
|
344 \left[\matrix{c_0^n & \ldots & c_0^2 & c_0 & 1\cr |
|
345 c_1^n & \ldots & c_1^2 & c_1 & 1\cr |
|
346 \vdots & & \vdots & \vdots & \vdots\cr |
|
347 c_n^n & \ldots & c_n^2 & c_n & 1}\right]. |
|
348 $$ |
|
349 @end tex |
|
350 @end iftex |
|
351 @ifinfo |
|
352 |
|
353 @example |
|
354 @group |
|
355 c(0)^n ... c(0)^2 c(0) 1 |
|
356 c(1)^n ... c(1)^2 c(1) 1 |
|
357 . . . . |
|
358 . . . . |
|
359 . . . . |
|
360 |
|
361 c(n)^n ... c(n)^2 c(n) 1 |
|
362 @end group |
|
363 @end example |
|
364 @end ifinfo |
2449
|
365 @end deftypefn |