7017
|
1 ## Copyright (C) 2000, 2005, 2006, 2007 Paul Kienzle |
5178
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5181
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5178
|
18 |
|
19 ## -*- texinfo -*- |
5182
|
20 ## @deftypefn {Function File} {} setdiff (@var{a}, @var{b}) |
6248
|
21 ## @deftypefnx {Function File} {} setdiff (@var{a}, @var{b}, "rows") |
5786
|
22 ## Return the elements in @var{a} that are not in @var{b}, sorted in |
5182
|
23 ## ascending order. If @var{a} and @var{b} are both column vectors |
|
24 ## return a column vector, otherwise return a row vector. |
5786
|
25 ## |
|
26 ## Given the optional third argument @samp{"rows"}, return the rows in |
|
27 ## @var{a} that are not in @var{b}, sorted in ascending order by rows. |
5642
|
28 ## @seealso{unique, union, intersect, setxor, ismember} |
5178
|
29 ## @end deftypefn |
|
30 |
5181
|
31 ## Author: Paul Kienzle |
|
32 ## Adapted-by: jwe |
|
33 |
6385
|
34 function c = setdiff (a, b, byrows_arg) |
5786
|
35 |
|
36 if (nargin < 2 || nargin > 3) |
6046
|
37 print_usage (); |
5786
|
38 endif |
5181
|
39 |
6385
|
40 byrows = false; |
|
41 |
5786
|
42 if (nargin == 3) |
6385
|
43 if (! strcmpi (byrows_arg, "rows")) |
5786
|
44 error ("expecting third argument to be \"rows\""); |
6385
|
45 elseif (iscell (a) || iscell (b)) |
|
46 warning ("setdiff: \"rows\" not valid for cell arrays"); |
5786
|
47 else |
|
48 byrows = true; |
|
49 endif |
5178
|
50 endif |
|
51 |
5786
|
52 if (byrows) |
|
53 c = unique (a, "rows"); |
|
54 if (! isempty (c) && ! isempty (b)) |
|
55 ## Form a and b into combined set. |
|
56 b = unique (b, "rows"); |
|
57 [dummy, idx] = sortrows ([c; b]); |
|
58 ## Eliminate those elements of a that are the same as in b. |
6386
|
59 dups = find (all (dummy(1:end-1,:) == dummy(2:end,:), 2)); |
|
60 c(idx(dups),:) = []; |
5786
|
61 endif |
|
62 else |
|
63 c = unique (a); |
|
64 if (! isempty (c) && ! isempty (b)) |
|
65 ## Form a and b into combined set. |
|
66 b = unique (b); |
|
67 [dummy, idx] = sort ([c(:); b(:)]); |
|
68 ## Eliminate those elements of a that are the same as in b. |
6385
|
69 if (iscellstr (dummy)) |
6386
|
70 dups = find (strcmp (dummy(1:end-1), dummy(2:end))); |
6385
|
71 else |
6386
|
72 dups = find (dummy(1:end-1) == dummy(2:end)); |
6385
|
73 endif |
6386
|
74 c(idx(dups)) = []; |
5786
|
75 ## Reshape if necessary. |
|
76 if (size (c, 1) != 1 && size (b, 1) == 1) |
|
77 c = c.'; |
|
78 endif |
5178
|
79 endif |
|
80 endif |
5786
|
81 |
5178
|
82 endfunction |
5642
|
83 |
5786
|
84 %!assert(setdiff(["bb";"zz";"bb";"zz"],["bb";"cc";"bb"],"rows"), "zz") |
|
85 %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"],"rows"), "z") |
|
86 %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"]), "z") |
|
87 %!assert(setdiff([1, 1; 2, 2; 3, 3; 4, 4], [1, 1; 2, 2; 4, 4], "rows"), [3 3]) |
|
88 %!assert(setdiff([1; 2; 3; 4], [1; 2; 4], "rows"), 3) |
6386
|
89 %!assert(setdiff([1, 2; 3, 4], [1, 2; 3, 6], "rows"), [3, 4]) |
6385
|
90 %!assert(setdiff({"one","two";"three","four"},{"one","two";"three","six"}), {"four"}) |