annotate scripts/set/setxor.m @ 11922:746f13936eee release-3-0-x

improve set functions for Matlab compatibility
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 16 Jan 2009 08:10:28 +0100
parents a1dbe9d80eee
children cadc73247d65
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 2000, 2006, 2007 Paul Kienzle
11922
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
2 ## Copyright (C) 2008 Jaroslav Hajek
5825
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
3 ##
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
5 ##
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
9 ## your option) any later version.
5825
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
10 ##
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
15 ##
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5825
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
19
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
21 ## @deftypefn {Function File} {} setxor (@var{a}, @var{b})
11922
746f13936eee 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
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
23 ##
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
24 ## Return the elements exclusive to @var{a} or @var{b}, sorted in ascending
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
25 ## order. If @var{a} and @var{b} are both column vectors return a column
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
26 ## vector, otherwise return a row vector.
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
27 ##
11922
746f13936eee 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})
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
29 ##
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
30 ## Return index vectors @var{ia} and @var{ib} such that @code{a==c(ia)} and
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
31 ## @code{b==c(ib)}.
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
32 ##
5825
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
33 ## @seealso{unique, union, intersect, setdiff, ismember}
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
34 ## @end deftypefn
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
35
11922
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
36 function [c, ia, ib] = setxor (a, b, varargin)
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
37
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
38 if (nargin < 2 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5825
diff changeset
39 print_usage ();
5825
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
40 endif
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
41
11922
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
42 if (nargin == 3 && ! strcmpi (varargin{1}, "rows"))
746f13936eee 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'");
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
44 endif
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
45
5825
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
46 ## Form A and B into sets.
11922
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
47 if (nargout > 1)
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
48 [a, ia] = unique (a, varargin{:});
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
49 [b, ib] = unique (b, varargin{:});
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
50 else
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
51 a = unique (a, varargin{:});
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
52 b = unique (b, varargin{:});
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
53 endif
5825
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
54
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
55 if (isempty (a))
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
56 c = b;
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
57 elseif (isempty (b))
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
58 c = a;
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
59 else
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
60 ## Reject duplicates.
11922
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
61 if (nargin > 2)
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
62 na = rows (a); nb = rows (b);
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
63 [c, i] = sortrows ([a; b]);
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
64 n = rows (c);
746f13936eee 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));
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
66 if (! isempty (idx))
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
67 c([idx, idx+1],:) = [];
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
68 i([idx, idx+1],:) = [];
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
69 endif
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
70 else
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
71 na = numel (a); nb = numel (b);
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
72 [c, i] = sort ([a(:); b(:)]);
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
73 n = length (c);
746f13936eee 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));
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
75 if (! isempty (idx))
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
76 c([idx, idx+1]) = [];
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
77 i([idx, idx+1]) = [];
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
78 endif
746f13936eee 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)
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
80 c = c.';
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
81 endif
5825
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
82 endif
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
83 endif
11922
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
84 if (nargout > 1)
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
85 ia = ia(i(i <= na));
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
86 ib = ib(i(i > na) - na);
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
87 endif
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
88
5825
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
89 endfunction
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
90
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
91 %!assert(setxor([1,2,3],[2,3,4]),[1,4])
11922
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
92 %!test
746f13936eee 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];
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
94 %! [y, ia, ib] = setxor (a, b.');
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
95 %! assert(y, [2, 5]);
746f13936eee improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
96 %! assert(y, sort([a(ia), b(ib)]));