diff scripts/general/triu.m @ 4:b4df021f796c

[project @ 1993-08-08 01:26:08 by jwe] Initial revision
author jwe
date Sun, 08 Aug 1993 01:26:08 +0000
parents
children 16a24e76d6e0
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/scripts/general/triu.m
@@ -0,0 +1,32 @@
+function retval = triu (x, k)
+
+# usage: triu (x, k)
+#
+# Return the upper triangular part of x above the k-th diagonal.  If
+# the second argument is omitted, k = 0 is assumed.
+#
+# See also: tril, diag
+
+  if (nargin > 0)
+    [nr, nc] = size (x);
+    retval = x;
+  endif
+
+  if (nargin == 1)
+    k = 0;
+  elseif (nargin == 2)
+    max_nr_nc = max (nr, nc);
+    if ((k > 0 && k > nc - 1) || (k < 0 && k < 1 - nr))
+      error ("triu: requested diagonal out of range")
+    endif
+  else
+    error ("usage: triu (x [, k])");
+  endif
+
+  for j = 1:nc
+    for i = j+1-k:nr
+      retval (i, j) = 0.0;
+    endfor
+  endfor
+
+endfunction