Mercurial > hg > octave-lyh
annotate scripts/set/setdiff.m @ 9051:1bf0ce0930be
Grammar check TexInfo in all .m files
Cleanup documentation sources to follow a few consistent rules.
Spellcheck was NOT done. (but will be in another changeset)
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Fri, 27 Mar 2009 22:31:03 -0700 |
parents | e56bb65186f6 |
children | 16f53d29049f |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2000, 2005, 2006, 2007 Paul Kienzle |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
2 ## Copyright (C) 2008 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 -*- | |
5182 | 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. | |
5786 | 27 ## |
28 ## Given the optional third argument @samp{"rows"}, return the rows in | |
29 ## @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
|
30 ## |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
31 ## If requested, return @var{i} such that @code{c = a(i)}. |
5642 | 32 ## @seealso{unique, union, intersect, setxor, ismember} |
5178 | 33 ## @end deftypefn |
34 | |
5181 | 35 ## Author: Paul Kienzle |
36 ## Adapted-by: jwe | |
37 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
38 function [c, i] = setdiff (a, b, byrows_arg) |
5786 | 39 |
40 if (nargin < 2 || nargin > 3) | |
6046 | 41 print_usage (); |
5786 | 42 endif |
5181 | 43 |
6385 | 44 byrows = false; |
45 | |
5786 | 46 if (nargin == 3) |
6385 | 47 if (! strcmpi (byrows_arg, "rows")) |
5786 | 48 error ("expecting third argument to be \"rows\""); |
6385 | 49 elseif (iscell (a) || iscell (b)) |
50 warning ("setdiff: \"rows\" not valid for cell arrays"); | |
5786 | 51 else |
52 byrows = true; | |
53 endif | |
5178 | 54 endif |
55 | |
5786 | 56 if (byrows) |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
57 if (nargout > 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
58 [c, i] = unique (a, "rows"); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
59 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
60 c = unique (a, "rows"); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
61 endif |
5786 | 62 if (! isempty (c) && ! isempty (b)) |
63 ## Form a and b into combined set. | |
64 b = unique (b, "rows"); | |
65 [dummy, idx] = sortrows ([c; b]); | |
66 ## Eliminate those elements of a that are the same as in b. | |
6386 | 67 dups = find (all (dummy(1:end-1,:) == dummy(2:end,:), 2)); |
68 c(idx(dups),:) = []; | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
69 if (nargout > 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
70 i(idx(dups),:) = []; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
71 endif |
5786 | 72 endif |
73 else | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
74 if (nargout > 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
75 [c, i] = unique (a); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
76 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
77 c = unique (a); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
78 endif |
5786 | 79 if (! isempty (c) && ! isempty (b)) |
80 ## Form a and b into combined set. | |
81 b = unique (b); | |
82 [dummy, idx] = sort ([c(:); b(:)]); | |
83 ## Eliminate those elements of a that are the same as in b. | |
6385 | 84 if (iscellstr (dummy)) |
6386 | 85 dups = find (strcmp (dummy(1:end-1), dummy(2:end))); |
6385 | 86 else |
6386 | 87 dups = find (dummy(1:end-1) == dummy(2:end)); |
6385 | 88 endif |
6386 | 89 c(idx(dups)) = []; |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
90 if (nargout > 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
91 i(idx(dups)) = []; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
92 endif |
5786 | 93 ## Reshape if necessary. |
94 if (size (c, 1) != 1 && size (b, 1) == 1) | |
95 c = c.'; | |
96 endif | |
5178 | 97 endif |
98 endif | |
5786 | 99 |
5178 | 100 endfunction |
5642 | 101 |
5786 | 102 %!assert(setdiff(["bb";"zz";"bb";"zz"],["bb";"cc";"bb"],"rows"), "zz") |
103 %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"],"rows"), "z") | |
104 %!assert(setdiff(["b";"z";"b";"z"],["b";"c";"b"]), "z") | |
105 %!assert(setdiff([1, 1; 2, 2; 3, 3; 4, 4], [1, 1; 2, 2; 4, 4], "rows"), [3 3]) | |
106 %!assert(setdiff([1; 2; 3; 4], [1; 2; 4], "rows"), 3) | |
6386 | 107 %!assert(setdiff([1, 2; 3, 4], [1, 2; 3, 6], "rows"), [3, 4]) |
6385 | 108 %!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
|
109 |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
110 %!test |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
111 %! 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
|
112 %! [y, i] = setdiff (a, b.'); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
113 %! assert(y, [5]); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
114 %! assert(y, a(i)); |