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 |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
245
|
19 |
3369
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} tril (@var{a}, @var{k}) |
|
22 ## @deftypefnx {Function File} {} triu (@var{a}, @var{k}) |
|
23 ## Return a new matrix formed by extracting extract the lower (@code{tril}) |
|
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 |
|
62 ## @end deftypefn |
5053
|
63 ## |
3408
|
64 ## @seealso{triu and diag} |
4
|
65 |
2314
|
66 ## Author: jwe |
|
67 |
2311
|
68 function retval = tril (x, k) |
4
|
69 |
|
70 if (nargin > 0) |
|
71 [nr, nc] = size (x); |
1083
|
72 retval = zeros (nr, nc); |
4
|
73 endif |
|
74 |
|
75 if (nargin == 1) |
|
76 k = 0; |
|
77 elseif (nargin == 2) |
1083
|
78 if ((k > 0 && k > nc) || (k < 0 && k < -nr)) |
1518
|
79 error ("tril: requested diagonal out of range"); |
4
|
80 endif |
|
81 else |
3456
|
82 usage ("tril (x, k)"); |
4
|
83 endif |
|
84 |
2015
|
85 for j = 1 : min (nc, nr+k) |
1083
|
86 nr_limit = max (1, j-k); |
|
87 retval (nr_limit:nr, j) = x (nr_limit:nr, j); |
4
|
88 endfor |
|
89 |
|
90 endfunction |