comparison scripts/set/setdiff.m @ 5182:5b361aa47dff

[project @ 2005-03-03 06:21:47 by jwe]
author jwe
date Thu, 03 Mar 2005 06:21:47 +0000
parents 41cd70503c72
children 4c8a2e4e0717
comparison
equal deleted inserted replaced
5181:41cd70503c72 5182:5b361aa47dff
16 ## along with Octave; see the file COPYING. If not, write to the Free 16 ## along with Octave; see the file COPYING. If not, write to the Free
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA 17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
18 ## 02111-1307, USA. 18 ## 02111-1307, USA.
19 19
20 ## -*- texinfo -*- 20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} setdiff(@var{a}, @var{b}) 21 ## @deftypefn {Function File} {} setdiff (@var{a}, @var{b})
22 ## 22 ## Return the elements in @var{a} but not in @var{b}, sorted in
23 ## Return the elements in @var{a} but not in @var{b}, sorted in ascending 23 ## ascending order. If @var{a} and @var{b} are both column vectors
24 ## order. If @var{a} and @var{b} are both column vectors return a column 24 ## return a column vector, otherwise return a row vector.
25 ## vector, otherwise return a row vector.
26 ##
27 ## @end deftypefn 25 ## @end deftypefn
28 ## @seealso{unique, union, intersect, setxor, ismember} 26 ## @seealso{unique, union, intersect, setxor, ismember}
29 27
30 ## Author: Paul Kienzle 28 ## Author: Paul Kienzle
31 ## Adapted-by: jwe 29 ## Adapted-by: jwe
36 usage ("setdiff (a, b)"); 34 usage ("setdiff (a, b)");
37 endif 35 endif
38 36
39 c = unique (a); 37 c = unique (a);
40 if (! isempty (c) && ! isempty (b)) 38 if (! isempty (c) && ! isempty (b))
41 ## form a and b into combined set 39 ## Form a and b into combined set.
42 b = unique (b); 40 b = unique (b);
43 [dummy, idx] = sort ([c(:); b(:)]); 41 [dummy, idx] = sort ([c(:); b(:)]);
44 ## eliminate those elements of a that are the same as in b 42 ## Eliminate those elements of a that are the same as in b.
45 n = length (dummy); 43 n = length (dummy);
46 c(idx(find (dummy(1:n-1) == dummy(2:n)))) = []; 44 c(idx(find (dummy(1:n-1) == dummy(2:n)))) = [];
47 ## reshape if necessary 45 ## Reshape if necessary.
48 if (size (c, 1) != 1 && size (b, 1) == 1) 46 if (size (c, 1) != 1 && size (b, 1) == 1)
49 c = c.'; 47 c = c.';
50 endif 48 endif
51 endif 49 endif
52 50