comparison scripts/set/setdiff.m @ 5178:6758c11b5b99

[project @ 2005-03-03 05:18:04 by jwe]
author jwe
date Thu, 03 Mar 2005 05:18:04 +0000
parents
children 41cd70503c72
comparison
equal deleted inserted replaced
5177:86fa011d6404 5178:6758c11b5b99
1 ## Copyright (C) 2000 Paul Kienzle
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {} setdiff(@var{a}, @var{b})
19 ##
20 ## Return the elements in @var{a} but not in @var{b}, sorted in ascending
21 ## order. If @var{a} and @var{b} are both column vectors return a column
22 ## vector, otherwise return a row vector.
23 ##
24 ## @end deftypefn
25 ## @seealso{unique, union, intersect, setxor, ismember}
26
27 function c = setdiff(a,b)
28 if nargin != 2
29 usage("setdiff(a,b)");
30 endif
31
32 c = unique(a);
33 if !isempty(c) && !isempty(b)
34 ## form a and b into combined set
35 b = unique(b);
36 [dummy, idx] = sort([ c(:) ; b(:)]);
37 ## eliminate those elements of a that are the same as in b
38 n = length(dummy);
39 c(idx(find(dummy(1:n-1) == dummy(2:n)))) = [];
40 ## reshape if necessary
41 if ( size(c,1) != 1 && size(b,1) == 1 )
42 c = c.';
43 endif
44 endif
45 endfunction
46