Mercurial > octave-dspies
annotate scripts/set/setdiff.m @ 19003:d00f6b09258f @
Overhaul functions in scripts/set directory.
* set.txi: Rewrite documentation for set functions.
* intersect.m: Rewrite docstring. Use by_rows variable for code clarity.
Return output orientation which is compatible with Matlab. Add %!tests for
output orientation and N-dimensional inputs.
* setdiff.m: Rewrite docstring. Use by_rows variable for code clarity.
Rename output i to ia to clarify it is an index into the set a.
Return output orientation which is compatible with Matlab. Add %!tests for
N-dimensional inputs.
* setxor.m: Rewrite docstring. Use by_rows variable for code clarity.
Return output orientation which is compatible with Matlab. Add %!tests for
output orientation and N-dimensional inputs.
* union.m: Rewrite docstring. Use by_rows variable for code clarity.
Return output orientation which is compatible with Matlab. Add %!tests for
output orientation and N-dimensional inputs. Add %!tests for validsetargs
which are common to all set functions.
* unique.m: Rewrite docstring. Verify that input is numeric or cell array
of strings. Avoid computing idx for optional i,j outputs unless required.
Add %!error tests for input validation.
* ismember.m: Rewrite docstring. Use input variable 'a' instead of 'A' for
conformance with rest of set functions. Rename output index variable to
s_idx for clarity that it is an index into the set s.
* powerset.m: Rewrite doctring. Add input validation on nargin. Add %!error
input validation tests.
* module.mk: Include validsetargs.m in build system.
* validsetargs.m: Function renamed from validargs which was too general.
* validargs.m: Function renamed to validsetargs.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 11 Aug 2014 09:39:45 -0700 |
parents | 30d8501a857a |
children |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17281
diff
changeset
|
1 ## Copyright (C) 2000-2013 Paul Kienzle |
11523 | 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 -*- | |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
21 ## @deftypefn {Function File} {@var{c} =} setdiff (@var{a}, @var{b}) |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{c} =} setdiff (@var{a}, @var{b}, "rows") |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
23 ## @deftypefnx {Function File} {[@var{c}, @var{ia}] =} setdiff (@dots{}) |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
24 ## Return the elements in @var{a} that are not in @var{b} sorted in |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
25 ## ascending order. |
5786 | 26 ## |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
27 ## If @var{a} and @var{b} are both column vectors return a column vector; |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
28 ## Otherwise, return a row vector. The inputs may also be cell arrays of |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
29 ## strings. |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
30 ## |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
31 ## If the optional input @qcode{"rows"} is given then return the rows in |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
32 ## @var{a} that are not in @var{b}. The inputs must be 2-D matrices to use |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
33 ## this option. |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
34 ## |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
35 ## If requested, return the index vector @var{ia} such that |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
36 ## @code{@var{c} = @var{a}(@var{ia})}. |
5642 | 37 ## @seealso{unique, union, intersect, setxor, ismember} |
5178 | 38 ## @end deftypefn |
39 | |
5181 | 40 ## Author: Paul Kienzle |
41 ## Adapted-by: jwe | |
42 | |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
43 function [c, ia] = setdiff (a, b, varargin) |
5786 | 44 |
45 if (nargin < 2 || nargin > 3) | |
6046 | 46 print_usage (); |
5786 | 47 endif |
5181 | 48 |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
49 [a, b] = validsetargs ("setdiff", a, b, varargin{:}); |
6385 | 50 |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
51 by_rows = nargin == 3; |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
52 iscol = isvector (a) && isvector (b) && iscolumn (a) && iscolumn (b); |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
53 |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
54 if (by_rows) |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
55 if (nargout > 1) |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
56 [c, ia] = unique (a, "rows"); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
57 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
58 c = unique (a, "rows"); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
59 endif |
5786 | 60 if (! isempty (c) && ! isempty (b)) |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
61 ## Form A and B into combined set. |
5786 | 62 b = unique (b, "rows"); |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
63 [tmp, idx] = sortrows ([c; b]); |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
64 ## Eliminate those elements of A that are the same as in B. |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
65 dups = find (all (tmp(1:end-1,:) == tmp(2:end,:), 2)); |
6386 | 66 c(idx(dups),:) = []; |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
67 if (nargout > 1) |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
68 ia(idx(dups),:) = []; |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
69 endif |
5786 | 70 endif |
71 else | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
72 if (nargout > 1) |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
73 [c, ia] = unique (a); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
74 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
75 c = unique (a); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
76 endif |
5786 | 77 if (! isempty (c) && ! isempty (b)) |
78 ## Form a and b into combined set. | |
79 b = unique (b); | |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
80 [tmp, idx] = sort ([c(:); b(:)]); |
5786 | 81 ## Eliminate those elements of a that are the same as in b. |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
82 if (iscellstr (tmp)) |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
83 dups = find (strcmp (tmp(1:end-1), tmp(2:end))); |
6385 | 84 else |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
85 dups = find (tmp(1:end-1) == tmp(2:end)); |
6385 | 86 endif |
6386 | 87 c(idx(dups)) = []; |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
88 if (nargout > 1) |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
89 ia(idx(dups)) = []; |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
90 endif |
18843
30d8501a857a
setdiff.m: Orient output (row/column) the same as Matlab (bug #42577).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
91 ## Reshape if necessary for Matlab compatibility. |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
92 if (iscol) |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
93 c = c(:); |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
94 else |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
95 c = c(:).'; |
5786 | 96 endif |
5178 | 97 endif |
98 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
99 |
5178 | 100 endfunction |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
101 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
102 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
103 %!assert (setdiff (["bb";"zz";"bb";"zz"], ["bb";"cc";"bb"], "rows"), "zz") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
104 %!assert (setdiff (["b";"z";"b";"z"], ["b";"c";"b"], "rows"), "z") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
105 %!assert (setdiff (["b";"z";"b";"z"], ["b";"c";"b"]), "z") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
106 %!assert (setdiff ([1, 1; 2, 2; 3, 3; 4, 4], [1, 1; 2, 2; 4, 4], "rows"), [3 3]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
107 %!assert (setdiff ([1; 2; 3; 4], [1; 2; 4], "rows"), 3) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
108 %!assert (setdiff ([1, 2; 3, 4], [1, 2; 3, 6], "rows"), [3, 4]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
109 %!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
|
110 |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
111 %!test |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
112 %! a = [3, 1, 4, 1, 5]; |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
113 %! b = [1, 2, 3, 4]; |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
114 %! [c, ia] = setdiff (a, b'); |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
115 %! assert (c, [5]); |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
116 %! assert (c, a(ia)); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
117 |
18843
30d8501a857a
setdiff.m: Orient output (row/column) the same as Matlab (bug #42577).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
118 %% Test output orientation compatibility (bug #42577) |
30d8501a857a
setdiff.m: Orient output (row/column) the same as Matlab (bug #42577).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
119 %!assert (setdiff ([1:5], 2), [1,3,4,5]) |
30d8501a857a
setdiff.m: Orient output (row/column) the same as Matlab (bug #42577).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
120 %!assert (setdiff ([1:5]', 2), [1;3;4;5]) |
30d8501a857a
setdiff.m: Orient output (row/column) the same as Matlab (bug #42577).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
121 %!assert (setdiff ([1:5], [2:3]), [1,4,5]) |
30d8501a857a
setdiff.m: Orient output (row/column) the same as Matlab (bug #42577).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
122 %!assert (setdiff ([1:5], [2:3]'), [1,4,5]) |
30d8501a857a
setdiff.m: Orient output (row/column) the same as Matlab (bug #42577).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
123 %!assert (setdiff ([1:5]', [2:3]), [1,4,5]) |
30d8501a857a
setdiff.m: Orient output (row/column) the same as Matlab (bug #42577).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
124 %!assert (setdiff ([1:5]', [2:3]'), [1;4;5]) |
30d8501a857a
setdiff.m: Orient output (row/column) the same as Matlab (bug #42577).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
125 |
19003
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
126 %!test |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
127 %! a = rand (3,3,3); |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
128 %! b = a(1); |
d00f6b09258f
Overhaul functions in scripts/set directory.
Rik <rik@octave.org>
parents:
18843
diff
changeset
|
129 %! assert (setdiff (a, b), sort (a(2:end))); |
18843
30d8501a857a
setdiff.m: Orient output (row/column) the same as Matlab (bug #42577).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
130 |