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 |
6385
|
35 function c = setdiff (a, b, byrows_arg) |
5786
|
36 |
|
37 if (nargin < 2 || nargin > 3) |
6046
|
38 print_usage (); |
5786
|
39 endif |
5181
|
40 |
6385
|
41 byrows = false; |
|
42 |
5786
|
43 if (nargin == 3) |
6385
|
44 if (! strcmpi (byrows_arg, "rows")) |
5786
|
45 error ("expecting third argument to be \"rows\""); |
6385
|
46 elseif (iscell (a) || iscell (b)) |
|
47 warning ("setdiff: \"rows\" not valid for cell arrays"); |
5786
|
48 else |
|
49 byrows = true; |
|
50 endif |
5178
|
51 endif |
|
52 |
5786
|
53 if (byrows) |
|
54 c = unique (a, "rows"); |
|
55 if (! isempty (c) && ! isempty (b)) |
|
56 ## Form a and b into combined set. |
|
57 b = unique (b, "rows"); |
|
58 [dummy, idx] = sortrows ([c; b]); |
|
59 ## Eliminate those elements of a that are the same as in b. |
6386
|
60 dups = find (all (dummy(1:end-1,:) == dummy(2:end,:), 2)); |
|
61 c(idx(dups),:) = []; |
5786
|
62 endif |
|
63 else |
|
64 c = unique (a); |
|
65 if (! isempty (c) && ! isempty (b)) |
|
66 ## Form a and b into combined set. |
|
67 b = unique (b); |
|
68 [dummy, idx] = sort ([c(:); b(:)]); |
|
69 ## Eliminate those elements of a that are the same as in b. |
6385
|
70 if (iscellstr (dummy)) |
6386
|
71 dups = find (strcmp (dummy(1:end-1), dummy(2:end))); |
6385
|
72 else |
6386
|
73 dups = find (dummy(1:end-1) == dummy(2:end)); |
6385
|
74 endif |
6386
|
75 c(idx(dups)) = []; |
5786
|
76 ## Reshape if necessary. |
|
77 if (size (c, 1) != 1 && size (b, 1) == 1) |
|
78 c = c.'; |
|
79 endif |
5178
|
80 endif |
|
81 endif |
5786
|
82 |
5178
|
83 endfunction |
5642
|
84 |
5786
|
85 %!assert(setdiff(["bb";"zz";"bb";"zz"],["bb";"cc";"bb"],"rows"), "zz") |
|
86 %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"],"rows"), "z") |
|
87 %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"]), "z") |
|
88 %!assert(setdiff([1, 1; 2, 2; 3, 3; 4, 4], [1, 1; 2, 2; 4, 4], "rows"), [3 3]) |
|
89 %!assert(setdiff([1; 2; 3; 4], [1; 2; 4], "rows"), 3) |
6386
|
90 %!assert(setdiff([1, 2; 3, 4], [1, 2; 3, 6], "rows"), [3, 4]) |
6385
|
91 %!assert(setdiff({"one","two";"three","four"},{"one","two";"three","six"}), {"four"}) |