4
|
1 function retval = triu (x, k) |
|
2 |
|
3 # usage: triu (x, k) |
|
4 # |
|
5 # Return the upper triangular part of x above the k-th diagonal. If |
|
6 # the second argument is omitted, k = 0 is assumed. |
|
7 # |
|
8 # See also: tril, diag |
|
9 |
|
10 if (nargin > 0) |
|
11 [nr, nc] = size (x); |
|
12 retval = x; |
|
13 endif |
|
14 |
|
15 if (nargin == 1) |
|
16 k = 0; |
|
17 elseif (nargin == 2) |
|
18 max_nr_nc = max (nr, nc); |
|
19 if ((k > 0 && k > nc - 1) || (k < 0 && k < 1 - nr)) |
|
20 error ("triu: requested diagonal out of range") |
|
21 endif |
|
22 else |
|
23 error ("usage: triu (x [, k])"); |
|
24 endif |
|
25 |
|
26 for j = 1:nc |
|
27 for i = j+1-k:nr |
|
28 retval (i, j) = 0.0; |
|
29 endfor |
|
30 endfor |
|
31 |
|
32 endfunction |