1 ## Copyright (C) 2008-2012 Jaroslav Hajek
2 ## Copyright (C) 2000, 2006-2007 Paul Kienzle
4 ## This file is part of Octave.
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.
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
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/>.
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})
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).
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
34 ## @seealso{unique, union, intersect, setdiff, ismember}
37 function [c, ia, ib] = setxor (a, b, varargin)
39 if (nargin < 2 || nargin > 3)
43 [a, b] = validargs ("setxor", a, b, varargin{:});
45 ## Form A and B into sets.
47 [a, ia] = unique (a, varargin{:});
48 [b, ib] = unique (b, varargin{:});
50 a = unique (a, varargin{:});
51 b = unique (b, varargin{:});
61 na = rows (a); nb = rows (b);
62 [c, i] = sortrows ([a; b]);
64 idx = find (all (c(1:n-1) == c(2:n), 2));
66 c([idx, idx+1],:) = [];
67 i([idx, idx+1],:) = [];
70 na = numel (a); nb = numel (b);
71 [c, i] = sort ([a(:); b(:)]);
74 idx = find (strcmp (c(1:n-1), c(2:n)));
76 idx = find (c(1:n-1) == c(2:n));
82 if (size (a, 1) == 1 || size (b, 1) == 1)
89 ib = ib(i(i > na) - na);
94 %!assert(setxor([1,2,3],[2,3,4]),[1,4])
95 %!assert(setxor({'a'}, {'a', 'b'}), {'b'});
97 %! a = [3, 1, 4, 1, 5]; b = [1, 2, 3, 4];
98 %! [y, ia, ib] = setxor (a, b.');
100 %! assert(y, sort([a(ia), b(ib)]));