]> Creatis software - CreaPhase.git/blob - octave_packages/m/set/union.m
update packages
[CreaPhase.git] / octave_packages / m / set / union.m
1 ## Copyright (C) 1994-2012 John W. Eaton
2 ## Copyright (C) 2008-2009 Jaroslav Hajek
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
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
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
17 ## along with Octave; see the file COPYING.  If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn  {Function File} {} union (@var{a}, @var{b})
22 ## @deftypefnx {Function File} {} union (@var{a}, @var{b}, "rows")
23 ## Return the set of elements that are in either of the sets @var{a} and
24 ## @var{b}.  @var{a}, @var{b} may be cell arrays of string(s).
25 ## For example:
26 ##
27 ## @example
28 ## @group
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
35 ## the matrices @var{a} and @var{b} will be considered an element of sets.
36 ## For example:
37 ##
38 ## @example
39 ## @group
40 ## union ([1, 2; 2, 3], [1, 2; 3, 4], "rows")
41 ##    @result{}  1   2
42 ##        2   3
43 ##        3   4
44 ## @end group
45 ## @end example
46 ##
47 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} union (@var{a}, @var{b})
48 ##
49 ## Return index vectors @var{ia} and @var{ib} such that @code{a(ia)} and
50 ## @code{b(ib)} are disjoint sets whose union is @var{c}.
51 ##
52 ## @seealso{intersect, setdiff, unique}
53 ## @end deftypefn
54
55 ## Author: jwe
56
57 function [y, ia, ib] = union (a, b, varargin)
58
59   if (nargin < 2 || nargin > 3)
60     print_usage ();
61   endif
62
63   [a, b] = validargs ("union", a, b, varargin{:});
64
65   if (nargin == 2)
66     y = [a(:); b(:)];
67     na = numel (a); nb = numel (b);
68     if (size (a, 1) == 1 || size (b, 1) == 1)
69       y = y.';
70     endif
71   else
72     y = [a; b];
73     na = rows (a); nb = rows (b);
74   endif
75
76   if (nargout == 1)
77     y = unique (y, varargin{:});
78   else
79     [y, i] = unique (y, varargin{:});
80     ia = i(i <= na);
81     ib = i(i > na) - na;
82   endif
83
84 endfunction
85
86 %!assert(all (all (union ([1, 2, 4], [2, 3, 5]) == [1, 2, 3, 4, 5])));
87
88 %!assert(all (all (union ([1; 2; 4], [2, 3, 5]) == [1, 2, 3, 4, 5])));
89
90 %!assert(all (all (union ([1, 2, 3], [5; 7; 9]) == [1, 2, 3, 5, 7, 9])));
91
92 %!error union (1);
93
94 %!error union (1, 2, 3);
95
96 %!test
97 %! a = [3, 1, 4, 1, 5]; b = [1, 2, 3, 4];
98 %! [y, ia, ib] = union (a, b.');
99 %! assert(y, [1, 2, 3, 4, 5]);
100 %! assert(y, sort([a(ia), b(ib)]));