5178
|
1 ## Copyright (C) 2000 Paul Kienzle |
|
2 ## |
5181
|
3 ## This file is part of Octave. |
5178
|
4 ## |
5181
|
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. |
5178
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
5181
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
5178
|
19 |
|
20 ## -*- texinfo -*- |
5182
|
21 ## @deftypefn {Function File} {} setdiff (@var{a}, @var{b}) |
|
22 ## Return the elements in @var{a} but not in @var{b}, sorted in |
|
23 ## ascending order. If @var{a} and @var{b} are both column vectors |
|
24 ## return a column vector, otherwise return a row vector. |
5178
|
25 ## @end deftypefn |
|
26 ## @seealso{unique, union, intersect, setxor, ismember} |
|
27 |
5181
|
28 ## Author: Paul Kienzle |
|
29 ## Adapted-by: jwe |
|
30 |
|
31 function c = setdiff (a, b) |
|
32 |
|
33 if (nargin != 2) |
|
34 usage ("setdiff (a, b)"); |
5178
|
35 endif |
|
36 |
5181
|
37 c = unique (a); |
|
38 if (! isempty (c) && ! isempty (b)) |
5182
|
39 ## Form a and b into combined set. |
5181
|
40 b = unique (b); |
|
41 [dummy, idx] = sort ([c(:); b(:)]); |
5182
|
42 ## Eliminate those elements of a that are the same as in b. |
5181
|
43 n = length (dummy); |
|
44 c(idx(find (dummy(1:n-1) == dummy(2:n)))) = []; |
5182
|
45 ## Reshape if necessary. |
5181
|
46 if (size (c, 1) != 1 && size (b, 1) == 1) |
5178
|
47 c = c.'; |
|
48 endif |
|
49 endif |
5181
|
50 |
5178
|
51 endfunction |
|
52 |