7017
|
1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2005, 2006, |
|
2 ## 2007 John W. Eaton |
2313
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
2313
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
245
|
19 |
3458
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} triu (@var{a}, @var{k}) |
3369
|
22 ## See tril. |
3458
|
23 ## @end deftypefn |
4
|
24 |
2314
|
25 ## Author: jwe |
|
26 |
2311
|
27 function retval = triu (x, k) |
4
|
28 |
|
29 if (nargin > 0) |
|
30 [nr, nc] = size (x); |
5731
|
31 retval = resize (resize (x, 0), nr, nc); |
4
|
32 endif |
|
33 |
|
34 if (nargin == 1) |
|
35 k = 0; |
|
36 elseif (nargin == 2) |
1083
|
37 if ((k > 0 && k > nc) || (k < 0 && k < -nr)) |
1518
|
38 error ("triu: requested diagonal out of range"); |
4
|
39 endif |
|
40 else |
6046
|
41 print_usage (); |
4
|
42 endif |
|
43 |
2015
|
44 for j = max (1, k+1) : nc |
1083
|
45 nr_limit = min (nr, j-k); |
|
46 retval (1:nr_limit, j) = x (1:nr_limit, j); |
4
|
47 endfor |
|
48 |
|
49 endfunction |
7411
|
50 |
|
51 %!test |
|
52 %! a = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12]; |
|
53 %! |
|
54 %! u0 = [1, 2, 3; 0, 5, 6; 0, 0, 9; 0, 0, 0]; |
|
55 %! u1 = [0, 2, 3; 0, 0, 6; 0, 0, 0; 0, 0, 0]; |
|
56 %! u2 = [0, 0, 3; 0, 0, 0; 0, 0, 0; 0, 0, 0]; |
|
57 %! u3 = [0, 0, 0; 0, 0, 0; 0, 0, 0; 0, 0, 0]; |
|
58 %! um1 = [1, 2, 3; 4, 5, 6; 0, 8, 9; 0, 0, 12]; |
|
59 %! um2 = [1, 2, 3; 4, 5, 6; 7, 8, 9; 0, 11, 12]; |
|
60 %! um3 = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12]; |
|
61 %! |
|
62 %! assert((triu (a, -3) == um3 && triu (a, -2) == um2 |
|
63 %! && triu (a, -1) == um1 && triu (a) == u0 && triu (a, 1) == u1 |
|
64 %! && triu (a, 2) == u2 && triu (a, 3) == u3)); |
|
65 |
|
66 %!error triu (); |
|
67 |
|
68 %!error triu (1, 2, 3); |
|
69 |