]> Creatis software - CreaPhase.git/blob - octave_packages/m/set/setxor.m
update packages
[CreaPhase.git] / octave_packages / m / set / setxor.m
1 ## Copyright (C) 2008-2012 Jaroslav Hajek
2 ## Copyright (C) 2000, 2006-2007 Paul Kienzle
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 the
8 ## Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
10 ##
11 ## Octave is distributed in the hope that it will be useful, but WITHOUT
12 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 ## FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 ## for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING.  If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn  {Function File} {} setxor (@var{a}, @var{b})
22 ## @deftypefnx {Function File} {} setxor (@var{a}, @var{b}, 'rows')
23 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} setxor (@var{a}, @var{b})
24 ##
25 ## Return the elements exclusive to @var{a} or @var{b}, sorted in ascending
26 ## order.  If @var{a} and @var{b} are both column vectors return a column
27 ## vector, otherwise return a row vector.
28 ## @var{a}, @var{b} may be cell arrays of string(s).
29 ##
30 ## With three output arguments, return index vectors @var{ia} and @var{ib}
31 ## such that @code{a(ia)} and @code{b(ib)} are disjoint sets whose union
32 ## is @var{c}.
33 ##
34 ## @seealso{unique, union, intersect, setdiff, ismember}
35 ## @end deftypefn
36
37 function [c, ia, ib] = setxor (a, b, varargin)
38
39   if (nargin < 2 || nargin > 3)
40     print_usage ();
41   endif
42
43   [a, b] = validargs ("setxor", a, b, varargin{:});
44
45   ## Form A and B into sets.
46   if (nargout > 1)
47     [a, ia] = unique (a, varargin{:});
48     [b, ib] = unique (b, varargin{:});
49   else
50     a = unique (a, varargin{:});
51     b = unique (b, varargin{:});
52   endif
53
54   if (isempty (a))
55     c = b;
56   elseif (isempty (b))
57     c = a;
58   else
59     ## Reject duplicates.
60     if (nargin > 2)
61       na = rows (a); nb = rows (b);
62       [c, i] = sortrows ([a; b]);
63       n = rows (c);
64       idx = find (all (c(1:n-1) == c(2:n), 2));
65       if (! isempty (idx))
66         c([idx, idx+1],:) = [];
67         i([idx, idx+1],:) = [];
68       endif
69     else
70       na = numel (a); nb = numel (b);
71       [c, i] = sort ([a(:); b(:)]);
72       n = length (c);
73       if (iscell (c))
74         idx = find (strcmp (c(1:n-1), c(2:n)));
75       else
76         idx = find (c(1:n-1) == c(2:n));
77       endif
78       if (! isempty (idx))
79         c([idx, idx+1]) = [];
80         i([idx, idx+1]) = [];
81       endif
82       if (size (a, 1) == 1 || size (b, 1) == 1)
83         c = c.';
84       endif
85     endif
86   endif
87   if (nargout > 1)
88     ia = ia(i(i <= na));
89     ib = ib(i(i > na) - na);
90   endif
91
92 endfunction
93
94 %!assert(setxor([1,2,3],[2,3,4]),[1,4])
95 %!assert(setxor({'a'}, {'a', 'b'}), {'b'});
96 %!test
97 %! a = [3, 1, 4, 1, 5]; b = [1, 2, 3, 4];
98 %! [y, ia, ib] = setxor (a, b.');
99 %! assert(y, [2, 5]);
100 %! assert(y, sort([a(ia), b(ib)]));
101