Mercurial > hg > octave-lyh
annotate scripts/set/setdiff.m @ 11523:fd0a3ac60b0e
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 14 Jan 2011 05:47:45 -0500 |
parents | be55736a0783 |
children | c792872f8942 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2000-2011 Paul Kienzle |
2 ## Copyright (C) 2008-2009 Jaroslav Hajek | |
5178 | 3 ## |
5181 | 4 ## This file is part of Octave. |
5178 | 5 ## |
5181 | 6 ## Octave is free software; you can redistribute it and/or modify it |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5181 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
5178 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5178 | 19 |
20 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## @deftypefn {Function File} {} setdiff (@var{a}, @var{b}) |
6248 | 22 ## @deftypefnx {Function File} {} setdiff (@var{a}, @var{b}, "rows") |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7920
diff
changeset
|
23 ## @deftypefnx {Function File} {[@var{c}, @var{i}] =} setdiff (@var{a}, @var{b}) |
5786 | 24 ## Return the elements in @var{a} that are not in @var{b}, sorted in |
5182 | 25 ## ascending order. If @var{a} and @var{b} are both column vectors |
26 ## return a column vector, otherwise return a row vector. | |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
27 ## @var{a}, @var{b} may be cell arrays of string(s). |
5786 | 28 ## |
29 ## Given the optional third argument @samp{"rows"}, return the rows in | |
30 ## @var{a} that are not in @var{b}, sorted in ascending order by rows. | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
31 ## |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
32 ## If requested, return @var{i} such that @code{c = a(i)}. |
5642 | 33 ## @seealso{unique, union, intersect, setxor, ismember} |
5178 | 34 ## @end deftypefn |
35 | |
5181 | 36 ## Author: Paul Kienzle |
37 ## Adapted-by: jwe | |
38 | |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
39 function [c, i] = setdiff (a, b, varargin) |
5786 | 40 |
41 if (nargin < 2 || nargin > 3) | |
6046 | 42 print_usage (); |
5786 | 43 endif |
5181 | 44 |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
45 [a, b] = validargs ("setdiff", a, b, varargin{:}); |
6385 | 46 |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9245
diff
changeset
|
47 if (nargin > 2) |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
48 if (nargout > 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
49 [c, i] = unique (a, "rows"); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
50 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
51 c = unique (a, "rows"); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
52 endif |
5786 | 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. | |
6386 | 58 dups = find (all (dummy(1:end-1,:) == dummy(2:end,:), 2)); |
59 c(idx(dups),:) = []; | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
60 if (nargout > 1) |
10549 | 61 i(idx(dups),:) = []; |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
62 endif |
5786 | 63 endif |
64 else | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
65 if (nargout > 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
66 [c, i] = unique (a); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
67 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
68 c = unique (a); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
69 endif |
5786 | 70 if (! isempty (c) && ! isempty (b)) |
71 ## Form a and b into combined set. | |
72 b = unique (b); | |
73 [dummy, idx] = sort ([c(:); b(:)]); | |
74 ## Eliminate those elements of a that are the same as in b. | |
6385 | 75 if (iscellstr (dummy)) |
6386 | 76 dups = find (strcmp (dummy(1:end-1), dummy(2:end))); |
6385 | 77 else |
10549 | 78 dups = find (dummy(1:end-1) == dummy(2:end)); |
6385 | 79 endif |
6386 | 80 c(idx(dups)) = []; |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
81 if (nargout > 1) |
10549 | 82 i(idx(dups)) = []; |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
83 endif |
5786 | 84 ## Reshape if necessary. |
85 if (size (c, 1) != 1 && size (b, 1) == 1) | |
10549 | 86 c = c.'; |
5786 | 87 endif |
5178 | 88 endif |
89 endif | |
5786 | 90 |
5178 | 91 endfunction |
5642 | 92 |
5786 | 93 %!assert(setdiff(["bb";"zz";"bb";"zz"],["bb";"cc";"bb"],"rows"), "zz") |
94 %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"],"rows"), "z") | |
95 %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"]), "z") | |
96 %!assert(setdiff([1, 1; 2, 2; 3, 3; 4, 4], [1, 1; 2, 2; 4, 4], "rows"), [3 3]) | |
97 %!assert(setdiff([1; 2; 3; 4], [1; 2; 4], "rows"), 3) | |
6386 | 98 %!assert(setdiff([1, 2; 3, 4], [1, 2; 3, 6], "rows"), [3, 4]) |
6385 | 99 %!assert(setdiff({"one","two";"three","four"},{"one","two";"three","six"}), {"four"}) |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
100 |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
101 %!test |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
102 %! a = [3, 1, 4, 1, 5]; b = [1, 2, 3, 4]; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
103 %! [y, i] = setdiff (a, b.'); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
104 %! assert(y, [5]); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
105 %! assert(y, a(i)); |