2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
245
|
19 |
3369
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} tril (@var{a}, @var{k}) |
|
22 ## @deftypefnx {Function File} {} triu (@var{a}, @var{k}) |
5668
|
23 ## Return a new matrix formed by extracting the lower (@code{tril}) |
3369
|
24 ## or upper (@code{triu}) triangular part of the matrix @var{a}, and |
|
25 ## setting all other elements to zero. The second argument is optional, |
|
26 ## and specifies how many diagonals above or below the main diagonal should |
|
27 ## also be set to zero. |
3426
|
28 ## |
3369
|
29 ## The default value of @var{k} is zero, so that @code{triu} and |
|
30 ## @code{tril} normally include the main diagonal as part of the result |
|
31 ## matrix. |
3426
|
32 ## |
3369
|
33 ## If the value of @var{k} is negative, additional elements above (for |
|
34 ## @code{tril}) or below (for @code{triu}) the main diagonal are also |
|
35 ## selected. |
3426
|
36 ## |
3369
|
37 ## The absolute value of @var{k} must not be greater than the number of |
|
38 ## sub- or super-diagonals. |
3426
|
39 ## |
3369
|
40 ## For example, |
3426
|
41 ## |
3369
|
42 ## @example |
|
43 ## @group |
|
44 ## tril (ones (3), -1) |
|
45 ## @result{} 0 0 0 |
|
46 ## 1 0 0 |
|
47 ## 1 1 0 |
|
48 ## @end group |
|
49 ## @end example |
3426
|
50 ## |
3369
|
51 ## @noindent |
|
52 ## and |
3426
|
53 ## |
3369
|
54 ## @example |
|
55 ## @group |
|
56 ## tril (ones (3), 1) |
|
57 ## @result{} 1 1 0 |
|
58 ## 1 1 1 |
|
59 ## 1 1 1 |
|
60 ## @end group |
|
61 ## @end example |
5642
|
62 ## @seealso{triu, diag} |
3369
|
63 ## @end deftypefn |
4
|
64 |
2314
|
65 ## Author: jwe |
|
66 |
2311
|
67 function retval = tril (x, k) |
4
|
68 |
|
69 if (nargin > 0) |
|
70 [nr, nc] = size (x); |
5731
|
71 retval = resize (resize (x, 0), nr, nc); |
4
|
72 endif |
|
73 |
|
74 if (nargin == 1) |
|
75 k = 0; |
|
76 elseif (nargin == 2) |
1083
|
77 if ((k > 0 && k > nc) || (k < 0 && k < -nr)) |
1518
|
78 error ("tril: requested diagonal out of range"); |
4
|
79 endif |
|
80 else |
6046
|
81 print_usage (); |
4
|
82 endif |
|
83 |
2015
|
84 for j = 1 : min (nc, nr+k) |
1083
|
85 nr_limit = max (1, j-k); |
|
86 retval (nr_limit:nr, j) = x (nr_limit:nr, j); |
4
|
87 endfor |
|
88 |
|
89 endfunction |