5825
|
1 ## Copyright (C) 2000 Paul Kienzle |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} setxor (@var{a}, @var{b}) |
|
22 ## |
|
23 ## Return the elements exclusive to @var{a} or @var{b}, sorted in ascending |
|
24 ## order. If @var{a} and @var{b} are both column vectors return a column |
|
25 ## vector, otherwise return a row vector. |
|
26 ## |
|
27 ## @seealso{unique, union, intersect, setdiff, ismember} |
|
28 ## @end deftypefn |
|
29 |
|
30 function c = setxor (a, b) |
|
31 if (nargin != 2) |
6046
|
32 print_usage (); |
5825
|
33 endif |
|
34 |
|
35 ## Form A and B into sets. |
|
36 a = unique (a); |
|
37 b = unique (b); |
|
38 |
|
39 if (isempty (a)) |
|
40 c = b; |
|
41 elseif (isempty (b)) |
|
42 c = a; |
|
43 else |
|
44 ## Reject duplicates. |
|
45 c = sort ([a(:); b(:)]); |
|
46 n = length (c); |
|
47 idx = find (c(1:n-1) == c(2:n)); |
|
48 if (! isempty (idx)) |
|
49 c([idx, idx+1]) = []; |
|
50 endif |
|
51 if (size (a, 1) == 1 || size (b, 1) == 1) |
|
52 c = c.'; |
|
53 endif |
|
54 endif |
|
55 endfunction |
|
56 |
|
57 %!assert(setxor([1,2,3],[2,3,4]),[1,4]) |