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}) |
6248
|
22 ## @deftypefnx {Function File} {} setdiff (@var{a}, @var{b}, "rows") |
5786
|
23 ## Return the elements in @var{a} that are not in @var{b}, sorted in |
5182
|
24 ## ascending order. If @var{a} and @var{b} are both column vectors |
|
25 ## return a column vector, otherwise return a row vector. |
5786
|
26 ## |
|
27 ## Given the optional third argument @samp{"rows"}, return the rows in |
|
28 ## @var{a} that are not in @var{b}, sorted in ascending order by rows. |
5642
|
29 ## @seealso{unique, union, intersect, setxor, ismember} |
5178
|
30 ## @end deftypefn |
|
31 |
5181
|
32 ## Author: Paul Kienzle |
|
33 ## Adapted-by: jwe |
|
34 |
5786
|
35 function c = setdiff (a, b, byrows) |
|
36 |
|
37 if (nargin < 2 || nargin > 3) |
6046
|
38 print_usage (); |
5786
|
39 endif |
5181
|
40 |
5786
|
41 if (nargin == 3) |
|
42 if (! strcmpi (byrows, "rows")) |
|
43 error ("expecting third argument to be \"rows\""); |
|
44 else |
|
45 byrows = true; |
|
46 endif |
|
47 else |
|
48 byrows = false; |
5178
|
49 endif |
|
50 |
5786
|
51 if (byrows) |
|
52 c = unique (a, "rows"); |
|
53 if (! isempty (c) && ! isempty (b)) |
|
54 ## Form a and b into combined set. |
|
55 b = unique (b, "rows"); |
|
56 [dummy, idx] = sortrows ([c; b]); |
|
57 ## Eliminate those elements of a that are the same as in b. |
|
58 n = length (dummy); |
5967
|
59 c(idx(find (dummy(1:n-1) == dummy(2:n))), :) = []; |
5786
|
60 endif |
|
61 else |
|
62 c = unique (a); |
|
63 if (! isempty (c) && ! isempty (b)) |
|
64 ## Form a and b into combined set. |
|
65 b = unique (b); |
|
66 [dummy, idx] = sort ([c(:); b(:)]); |
|
67 ## Eliminate those elements of a that are the same as in b. |
|
68 n = length (dummy); |
5967
|
69 c(idx(find (dummy(1:n-1) == dummy(2:n)))) = []; |
5786
|
70 ## Reshape if necessary. |
|
71 if (size (c, 1) != 1 && size (b, 1) == 1) |
|
72 c = c.'; |
|
73 endif |
5178
|
74 endif |
|
75 endif |
5786
|
76 |
5178
|
77 endfunction |
5642
|
78 |
5786
|
79 %!assert(setdiff(["bb";"zz";"bb";"zz"],["bb";"cc";"bb"],"rows"), "zz") |
|
80 %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"],"rows"), "z") |
|
81 %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"]), "z") |
|
82 %!assert(setdiff([1, 1; 2, 2; 3, 3; 4, 4], [1, 1; 2, 2; 4, 4], "rows"), [3 3]) |
|
83 %!assert(setdiff([1; 2; 3; 4], [1; 2; 4], "rows"), 3) |