home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / general / tril.m < prev    next >
Encoding:
Text File  |  1999-11-21  |  2.4 KB  |  91 lines

  1. ## Copyright (C) 1996, 1997 John W. Eaton
  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.
  19.  
  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.
  28. ## 
  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.
  32. ## 
  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.
  36. ## 
  37. ## The absolute value of @var{k} must not be greater than the number of
  38. ## sub- or super-diagonals.
  39. ## 
  40. ## For example,
  41. ## 
  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
  50. ## 
  51. ## @noindent
  52. ## and
  53. ## 
  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
  63.  
  64. ## See also: triu, diag
  65.  
  66. ## Author: jwe
  67.  
  68. function retval = tril (x, k)
  69.  
  70.   if (nargin > 0)
  71.     [nr, nc] = size (x);
  72.     retval = zeros (nr, nc);
  73.   endif
  74.  
  75.   if (nargin == 1)
  76.     k = 0;
  77.   elseif (nargin == 2)
  78.     if ((k > 0 && k > nc) || (k < 0 && k < -nr))
  79.       error ("tril: requested diagonal out of range");
  80.     endif
  81.   else
  82.     usage ("tril (x [, k])");
  83.   endif
  84.  
  85.   for j = 1 : min (nc, nr+k)
  86.     nr_limit = max (1, j-k);
  87.     retval (nr_limit:nr, j) = x (nr_limit:nr, j);
  88.   endfor
  89.  
  90. endfunction
  91.