Mercurial > octave
comparison 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 |
comparison
equal
deleted
inserted
replaced
11921:166a195399f7 | 11922:746f13936eee |
---|---|
1 ## Copyright (C) 2000, 2006, 2007 Paul Kienzle | 1 ## Copyright (C) 2000, 2006, 2007 Paul Kienzle |
2 ## Copyright (C) 2008 Jaroslav Hajek | |
2 ## | 3 ## |
3 ## This file is part of Octave. | 4 ## This file is part of Octave. |
4 ## | 5 ## |
5 ## Octave is free software; you can redistribute it and/or modify it | 6 ## 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 ## under the terms of the GNU General Public License as published by |
16 ## along with Octave; see the file COPYING. If not, see | 17 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | 18 ## <http://www.gnu.org/licenses/>. |
18 | 19 |
19 ## -*- texinfo -*- | 20 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {} setxor (@var{a}, @var{b}) | 21 ## @deftypefn {Function File} {} setxor (@var{a}, @var{b}) |
22 ## @deftypefnx {Function File} {} setxor (@var{a}, @var{b}, 'rows') | |
21 ## | 23 ## |
22 ## Return the elements exclusive to @var{a} or @var{b}, sorted in ascending | 24 ## 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 | 25 ## order. If @var{a} and @var{b} are both column vectors return a column |
24 ## vector, otherwise return a row vector. | 26 ## vector, otherwise return a row vector. |
25 ## | 27 ## |
28 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} setxor (@var{a}, @var{b}) | |
29 ## | |
30 ## Return index vectors @var{ia} and @var{ib} such that @code{a==c(ia)} and | |
31 ## @code{b==c(ib)}. | |
32 ## | |
26 ## @seealso{unique, union, intersect, setdiff, ismember} | 33 ## @seealso{unique, union, intersect, setdiff, ismember} |
27 ## @end deftypefn | 34 ## @end deftypefn |
28 | 35 |
29 function c = setxor (a, b) | 36 function [c, ia, ib] = setxor (a, b, varargin) |
30 if (nargin != 2) | 37 |
38 if (nargin < 2 || nargin > 3) | |
31 print_usage (); | 39 print_usage (); |
32 endif | 40 endif |
33 | 41 |
42 if (nargin == 3 && ! strcmpi (varargin{1}, "rows")) | |
43 error ("setxor: if a third input argument is present, it must be the string 'rows'"); | |
44 endif | |
45 | |
34 ## Form A and B into sets. | 46 ## Form A and B into sets. |
35 a = unique (a); | 47 if (nargout > 1) |
36 b = unique (b); | 48 [a, ia] = unique (a, varargin{:}); |
49 [b, ib] = unique (b, varargin{:}); | |
50 else | |
51 a = unique (a, varargin{:}); | |
52 b = unique (b, varargin{:}); | |
53 endif | |
37 | 54 |
38 if (isempty (a)) | 55 if (isempty (a)) |
39 c = b; | 56 c = b; |
40 elseif (isempty (b)) | 57 elseif (isempty (b)) |
41 c = a; | 58 c = a; |
42 else | 59 else |
43 ## Reject duplicates. | 60 ## Reject duplicates. |
44 c = sort ([a(:); b(:)]); | 61 if (nargin > 2) |
45 n = length (c); | 62 na = rows (a); nb = rows (b); |
46 idx = find (c(1:n-1) == c(2:n)); | 63 [c, i] = sortrows ([a; b]); |
47 if (! isempty (idx)) | 64 n = rows (c); |
48 c([idx, idx+1]) = []; | 65 idx = find (all (c(1:n-1) == c(2:n), 2)); |
49 endif | 66 if (! isempty (idx)) |
50 if (size (a, 1) == 1 || size (b, 1) == 1) | 67 c([idx, idx+1],:) = []; |
51 c = c.'; | 68 i([idx, idx+1],:) = []; |
69 endif | |
70 else | |
71 na = numel (a); nb = numel (b); | |
72 [c, i] = sort ([a(:); b(:)]); | |
73 n = length (c); | |
74 idx = find (c(1:n-1) == c(2:n)); | |
75 if (! isempty (idx)) | |
76 c([idx, idx+1]) = []; | |
77 i([idx, idx+1]) = []; | |
78 endif | |
79 if (size (a, 1) == 1 || size (b, 1) == 1) | |
80 c = c.'; | |
81 endif | |
52 endif | 82 endif |
53 endif | 83 endif |
84 if (nargout > 1) | |
85 ia = ia(i(i <= na)); | |
86 ib = ib(i(i > na) - na); | |
87 endif | |
88 | |
54 endfunction | 89 endfunction |
55 | 90 |
56 %!assert(setxor([1,2,3],[2,3,4]),[1,4]) | 91 %!assert(setxor([1,2,3],[2,3,4]),[1,4]) |
92 %!test | |
93 %! a = [3, 1, 4, 1, 5]; b = [1, 2, 3, 4]; | |
94 %! [y, ia, ib] = setxor (a, b.'); | |
95 %! assert(y, [2, 5]); | |
96 %! assert(y, sort([a(ia), b(ib)])); |