Mercurial > hg > octave-nkf
annotate scripts/set/setxor.m @ 9023:60b7fb86380a
Added tag ss-3-1-55 for changeset 5e276a0b9997
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 25 Mar 2009 23:40:43 -0400 |
parents | eb63fbe60fab |
children | 1bf0ce0930be |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2008, 2009 Jaroslav Hajek |
7017 | 2 ## Copyright (C) 2000, 2006, 2007 Paul Kienzle |
5825 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
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. | |
5825 | 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. | |
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/>. | |
5825 | 19 |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {} setxor (@var{a}, @var{b}) | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
22 ## @deftypefnx {Function File} {} setxor (@var{a}, @var{b}, 'rows') |
5825 | 23 ## |
24 ## Return the elements exclusive to @var{a} or @var{b}, sorted in ascending | |
25 ## order. If @var{a} and @var{b} are both column vectors return a column | |
26 ## vector, otherwise return a row vector. | |
27 ## | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
28 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} setxor (@var{a}, @var{b}) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
29 ## |
8507 | 30 ## Return index vectors @var{ia} and @var{ib} such that @code{a == c(ia)} and |
31 ## @code{b == c(ib)}. | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
32 ## |
5825 | 33 ## @seealso{unique, union, intersect, setdiff, ismember} |
34 ## @end deftypefn | |
35 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
36 function [c, ia, ib] = setxor (a, b, varargin) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
37 |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
38 if (nargin < 2 || nargin > 3) |
6046 | 39 print_usage (); |
5825 | 40 endif |
41 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
42 if (nargin == 3 && ! strcmpi (varargin{1}, "rows")) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
43 error ("setxor: if a third input argument is present, it must be the string 'rows'"); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
44 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
45 |
5825 | 46 ## Form A and B into sets. |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
47 if (nargout > 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
48 [a, ia] = unique (a, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
49 [b, ib] = unique (b, varargin{:}); |
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 a = unique (a, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
52 b = unique (b, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
53 endif |
5825 | 54 |
55 if (isempty (a)) | |
56 c = b; | |
57 elseif (isempty (b)) | |
58 c = a; | |
59 else | |
60 ## Reject duplicates. | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
61 if (nargin > 2) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
62 na = rows (a); nb = rows (b); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
63 [c, i] = sortrows ([a; b]); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
64 n = rows (c); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
65 idx = find (all (c(1:n-1) == c(2:n), 2)); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
66 if (! isempty (idx)) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
67 c([idx, idx+1],:) = []; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
68 i([idx, idx+1],:) = []; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
69 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
70 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
71 na = numel (a); nb = numel (b); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
72 [c, i] = sort ([a(:); b(:)]); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
73 n = length (c); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
74 idx = find (c(1:n-1) == c(2:n)); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
75 if (! isempty (idx)) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
76 c([idx, idx+1]) = []; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
77 i([idx, idx+1]) = []; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
78 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
79 if (size (a, 1) == 1 || size (b, 1) == 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
80 c = c.'; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
81 endif |
5825 | 82 endif |
83 endif | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
84 if (nargout > 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
85 ia = ia(i(i <= na)); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
86 ib = ib(i(i > na) - na); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
87 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
88 |
5825 | 89 endfunction |
90 | |
91 %!assert(setxor([1,2,3],[2,3,4]),[1,4]) | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
92 %!test |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
93 %! 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
|
94 %! [y, ia, ib] = setxor (a, b.'); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
95 %! assert(y, [2, 5]); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
96 %! assert(y, sort([a(ia), b(ib)])); |