Mercurial > hg > octave-lyh
annotate scripts/set/setxor.m @ 11188:4cb1522e4d0f
Use function handle as input to cellfun,
rather than quoted function name or anonymous function wrapper.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 03 Nov 2010 17:20:56 -0700 |
parents | be55736a0783 |
children | fd0a3ac60b0e |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2008, 2009 Jaroslav Hajek |
7017 | 2 ## Copyright (C) 2000, 2006, 2007 Paul Kienzle |
5825 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5825 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5825 | 19 |
20 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## @deftypefn {Function File} {} setxor (@var{a}, @var{b}) |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
22 ## @deftypefnx {Function File} {} setxor (@var{a}, @var{b}, 'rows') |
5825 | 23 ## |
24 ## Return the elements exclusive to @var{a} or @var{b}, sorted in ascending | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
25 ## order. If @var{a} and @var{b} are both column vectors return a column |
5825 | 26 ## vector, otherwise return a row vector. |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9659
diff
changeset
|
27 ## @var{a}, @var{b} may be cell arrays of string(s). |
5825 | 28 ## |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
29 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} setxor (@var{a}, @var{b}) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
30 ## |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
31 ## Return index vectors @var{ia} and @var{ib} such that @code{a(ia)} and |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
32 ## @code{b(ib)} are |
9659
0bcfeadb6178
fix union, setxor docstrings
Jaroslav Hajek <highegg@gmail.com>
parents:
9498
diff
changeset
|
33 ## disjoint sets whose union is @var{c}. |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
34 ## |
5825 | 35 ## @seealso{unique, union, intersect, setdiff, ismember} |
36 ## @end deftypefn | |
37 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
38 function [c, ia, ib] = setxor (a, b, varargin) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
39 |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
40 if (nargin < 2 || nargin > 3) |
6046 | 41 print_usage (); |
5825 | 42 endif |
43 | |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9659
diff
changeset
|
44 [a, b] = validargs ("setxor", a, b, varargin{:}); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
45 |
5825 | 46 ## Form A and B into sets. |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
47 if (nargout > 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
48 [a, ia] = unique (a, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
49 [b, ib] = unique (b, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
50 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
51 a = unique (a, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
52 b = unique (b, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
53 endif |
5825 | 54 |
55 if (isempty (a)) | |
56 c = b; | |
57 elseif (isempty (b)) | |
58 c = a; | |
59 else | |
60 ## Reject duplicates. | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
61 if (nargin > 2) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
62 na = rows (a); nb = rows (b); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
63 [c, i] = sortrows ([a; b]); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
64 n = rows (c); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
65 idx = find (all (c(1:n-1) == c(2:n), 2)); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
66 if (! isempty (idx)) |
10549 | 67 c([idx, idx+1],:) = []; |
68 i([idx, idx+1],:) = []; | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
69 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
70 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
71 na = numel (a); nb = numel (b); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
72 [c, i] = sort ([a(:); b(:)]); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
73 n = length (c); |
9481
a3ae7abaf659
support cellstrs in setxor
Pieter Eendebak <pieter.eendebak@gmail.com>
parents:
9051
diff
changeset
|
74 if (iscell (c)) |
10549 | 75 idx = find (strcmp (c(1:n-1), c(2:n))); |
9481
a3ae7abaf659
support cellstrs in setxor
Pieter Eendebak <pieter.eendebak@gmail.com>
parents:
9051
diff
changeset
|
76 else |
a3ae7abaf659
support cellstrs in setxor
Pieter Eendebak <pieter.eendebak@gmail.com>
parents:
9051
diff
changeset
|
77 idx = find (c(1:n-1) == c(2:n)); |
a3ae7abaf659
support cellstrs in setxor
Pieter Eendebak <pieter.eendebak@gmail.com>
parents:
9051
diff
changeset
|
78 endif |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
79 if (! isempty (idx)) |
10549 | 80 c([idx, idx+1]) = []; |
81 i([idx, idx+1]) = []; | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
82 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
83 if (size (a, 1) == 1 || size (b, 1) == 1) |
10549 | 84 c = c.'; |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
85 endif |
5825 | 86 endif |
87 endif | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
88 if (nargout > 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
89 ia = ia(i(i <= na)); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
90 ib = ib(i(i > na) - na); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
91 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
92 |
5825 | 93 endfunction |
94 | |
95 %!assert(setxor([1,2,3],[2,3,4]),[1,4]) | |
9498
f7cc8f30f3b8
Added test for setxor
Pieter Eendebak <pieter.eendebak@gmail.com>
parents:
9481
diff
changeset
|
96 %!assert(setxor({'a'}, {'a', 'b'}), {'b'}); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
97 %!test |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
98 %! a = [3, 1, 4, 1, 5]; b = [1, 2, 3, 4]; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
99 %! [y, ia, ib] = setxor (a, b.'); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
100 %! assert(y, [2, 5]); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
101 %! assert(y, sort([a(ia), b(ib)])); |
9498
f7cc8f30f3b8
Added test for setxor
Pieter Eendebak <pieter.eendebak@gmail.com>
parents:
9481
diff
changeset
|
102 |