Mercurial > hg > octave-nkf
annotate scripts/set/union.m @ 8898:ca032de5fbf2
Remove references to deprecated function create_set.
author | Thorsten Meyer <thorsten.meyier@gmx.de> |
---|---|
date | Mon, 02 Mar 2009 10:22:17 +0100 |
parents | cadc73247d65 |
children | eb63fbe60fab |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1996, 1997, 1999, 2000, 2003, 2004, 2005, 2006, |
7344 | 2 ## 2007, 2008 John W. Eaton |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
3 ## Copyright (C) 2008 Jaroslav Hajek |
2313 | 4 ## |
5 ## This file is part of Octave. | |
6 ## | |
7 ## Octave is free software; you can redistribute it and/or modify it | |
8 ## under the terms of the GNU General Public License as published by | |
7016 | 9 ## the Free Software Foundation; either version 3 of the License, or (at |
10 ## your option) any later version. | |
2313 | 11 ## |
12 ## Octave is distributed in the hope that it will be useful, but | |
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 ## General Public License for more details. | |
16 ## | |
17 ## You should have received a copy of the GNU General Public License | |
7016 | 18 ## along with Octave; see the file COPYING. If not, see |
19 ## <http://www.gnu.org/licenses/>. | |
2303 | 20 |
3368 | 21 ## -*- texinfo -*- |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
22 ## @deftypefn {Function File} {} union (@var{a}, @var{b}) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
23 ## @deftypefnx{Function File} {} union (@var{a}, @var{b}, "rows") |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
24 ## Return the set of elements that are in either of the sets @var{a} and |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
25 ## @var{b}. For example, |
3426 | 26 ## |
3368 | 27 ## @example |
28 ## @group | |
7344 | 29 ## union ([1, 2, 4], [2, 3, 5]) |
30 ## @result{} [1, 2, 3, 4, 5] | |
31 ## @end group | |
32 ## @end example | |
33 ## | |
34 ## If the optional third input argument is the string "rows" each row of | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
35 ## the matrices @var{a} and @var{b} will be considered an element of sets. |
7344 | 36 ## For example, |
37 ## @example | |
38 ## @group | |
39 ## union([1, 2; 2, 3], [1, 2; 3, 4], "rows") | |
40 ## @result{} 1 2 | |
41 ## 2 3 | |
42 ## 3 4 | |
3368 | 43 ## @end group |
44 ## @end example | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
45 ## |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
46 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} union (@var{a}, @var{b}) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
47 ## |
8507 | 48 ## Return index vectors @var{ia} and @var{ib} such that @code{a == c(ia)} and |
49 ## @code{b == c(ib)}. | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
50 ## |
8898
ca032de5fbf2
Remove references to deprecated function create_set.
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8507
diff
changeset
|
51 ## @seealso{intersect, complement, unique} |
3406 | 52 ## @end deftypefn |
3368 | 53 |
2314 | 54 ## Author: jwe |
55 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
56 function [y, ia, ib] = union (a, b, varargin) |
559 | 57 |
7344 | 58 if (nargin < 2 || nargin > 3) |
6046 | 59 print_usage (); |
559 | 60 endif |
61 | |
7344 | 62 if (nargin == 3 && ! strcmpi (varargin{1}, "rows")) |
63 error ("union: if a third input argument is present, it must be the string 'rows'"); | |
64 endif | |
65 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
66 if (nargin == 2) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
67 y = [a(:); b(:)]; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
68 na = numel (a); nb = numel (b); |
4317 | 69 if (size (a, 1) == 1 || size (b, 1) == 1) |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
70 y = y.'; |
4317 | 71 endif |
7344 | 72 elseif (ndims (a) == 2 && ndims (b) == 2 && columns (a) == columns (b)) |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
73 y = [a; b]; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
74 na = rows (a); nb = rows (b); |
7344 | 75 else |
76 error ("union: input arguments must contain the same number of columns when \"rows\" is specified"); | |
559 | 77 endif |
78 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
79 if (nargout == 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
80 y = unique (y, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
81 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
82 [y, i] = unique (y, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
83 ia = i(i <= na); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
84 ib = i(i > na) - na; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
85 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
86 |
559 | 87 endfunction |
7411 | 88 |
89 %!assert(all (all (union ([1, 2, 4], [2, 3, 5]) == [1, 2, 3, 4, 5]))); | |
90 | |
91 %!assert(all (all (union ([1; 2; 4], [2, 3, 5]) == [1, 2, 3, 4, 5]))); | |
92 | |
93 %!assert(all (all (union ([1, 2, 3], [5; 7; 9]) == [1, 2, 3, 5, 7, 9]))); | |
94 | |
95 %!error union (1); | |
96 | |
97 %!error union (1, 2, 3); | |
98 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
99 %!test |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
100 %! a = [3, 1, 4, 1, 5]; b = [1, 2, 3, 4]; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
101 %! [y, ia, ib] = union (a, b.'); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
102 %! assert(y, [1, 2, 3, 4, 5]); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
103 %! assert(y, sort([a(ia), b(ib)])); |