7017
|
1 ## Copyright (C) 2000, 2006, 2007 Paul Kienzle |
5825
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5825
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5825
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} setxor (@var{a}, @var{b}) |
|
21 ## |
|
22 ## Return the elements exclusive to @var{a} or @var{b}, sorted in ascending |
|
23 ## order. If @var{a} and @var{b} are both column vectors return a column |
|
24 ## vector, otherwise return a row vector. |
|
25 ## |
|
26 ## @seealso{unique, union, intersect, setdiff, ismember} |
|
27 ## @end deftypefn |
|
28 |
|
29 function c = setxor (a, b) |
|
30 if (nargin != 2) |
6046
|
31 print_usage (); |
5825
|
32 endif |
|
33 |
|
34 ## Form A and B into sets. |
|
35 a = unique (a); |
|
36 b = unique (b); |
|
37 |
|
38 if (isempty (a)) |
|
39 c = b; |
|
40 elseif (isempty (b)) |
|
41 c = a; |
|
42 else |
|
43 ## Reject duplicates. |
|
44 c = sort ([a(:); b(:)]); |
|
45 n = length (c); |
|
46 idx = find (c(1:n-1) == c(2:n)); |
|
47 if (! isempty (idx)) |
|
48 c([idx, idx+1]) = []; |
|
49 endif |
|
50 if (size (a, 1) == 1 || size (b, 1) == 1) |
|
51 c = c.'; |
|
52 endif |
|
53 endif |
|
54 endfunction |
|
55 |
|
56 %!assert(setxor([1,2,3],[2,3,4]),[1,4]) |