home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / set / complement.m next >
Text File  |  1999-11-20  |  2KB  |  61 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} {} complement (@var{x}, @var{y})
  22. ## Return the elements of set @var{y} that are not in set @var{x}.  For
  23. ## example,
  24. ## 
  25. ## @example
  26. ## @group
  27. ## complement ([ 1, 2, 3 ], [ 2, 3, 5 ])
  28. ##      @result{} 5
  29. ## @end group
  30. ## @end example
  31. ## @end deftypefn
  32.  
  33. ## See also: create_set, union, intersection
  34.  
  35. ## Author: jwe
  36.  
  37. function y = complement (a, b)
  38.  
  39.   if (nargin != 2)
  40.     usage ("complement(a,b)");
  41.   endif
  42.  
  43.   if (isempty (a))
  44.     y = create_set(b);
  45.   elseif (isempty (b))
  46.     y = [];
  47.   else
  48.     a = create_set (a);
  49.     b = create_set (b);
  50.     yindex = 1;
  51.     y = zeros (1, length (b));
  52.     for index = 1:length (b)
  53.       if (all (a != b (index)))
  54.         y(yindex++) = b(index);
  55.       endif
  56.     endfor
  57.     y = y(1:(yindex-1));
  58.   endif
  59.  
  60. endfunction
  61.