Mercurial > hg > octave-lyh
annotate scripts/set/union.m @ 11477:a02d00dd3d5f
expm.m: new tests
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 10 Jan 2011 14:50:33 -0500 |
parents | 693e22af08ae |
children | fd0a3ac60b0e |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2008, 2009 Jaroslav Hajek |
7017 | 2 ## Copyright (C) 1994, 1996, 1997, 1999, 2000, 2003, 2004, 2005, 2006, |
8920 | 3 ## 2007 John W. Eaton |
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 -*- |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10712
diff
changeset
|
22 ## @deftypefn {Function File} {} union (@var{a}, @var{b}) |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10712
diff
changeset
|
23 ## @deftypefnx {Function File} {} union (@var{a}, @var{b}, "rows") |
7920
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 |
10711
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10088
diff
changeset
|
25 ## @var{b}. @var{a}, @var{b} may be cell arrays of string(s). |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
26 ## For example: |
3426 | 27 ## |
3368 | 28 ## @example |
29 ## @group | |
7344 | 30 ## union ([1, 2, 4], [2, 3, 5]) |
31 ## @result{} [1, 2, 3, 4, 5] | |
32 ## @end group | |
33 ## @end example | |
34 ## | |
35 ## 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
|
36 ## the matrices @var{a} and @var{b} will be considered an element of sets. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
37 ## For example: |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
38 ## |
7344 | 39 ## @example |
40 ## @group | |
41 ## union([1, 2; 2, 3], [1, 2; 3, 4], "rows") | |
42 ## @result{} 1 2 | |
43 ## 2 3 | |
44 ## 3 4 | |
3368 | 45 ## @end group |
46 ## @end example | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
47 ## |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
48 ## @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
|
49 ## |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10712
diff
changeset
|
50 ## 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:
10712
diff
changeset
|
51 ## @code{b(ib)} are |
9659
0bcfeadb6178
fix union, setxor docstrings
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
52 ## disjoint sets whose union is @var{c}. |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
53 ## |
10712
6a5a0c9df5d7
union.m: Remove seealso reference to deprecated function.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
54 ## @seealso{intersect, setdiff, unique} |
3406 | 55 ## @end deftypefn |
3368 | 56 |
2314 | 57 ## Author: jwe |
58 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
59 function [y, ia, ib] = union (a, b, varargin) |
559 | 60 |
7344 | 61 if (nargin < 2 || nargin > 3) |
6046 | 62 print_usage (); |
559 | 63 endif |
64 | |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9659
diff
changeset
|
65 [a, b] = validargs ("union", a, b, varargin{:}); |
7344 | 66 |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
67 if (nargin == 2) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
68 y = [a(:); b(:)]; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
69 na = numel (a); nb = numel (b); |
4317 | 70 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
|
71 y = y.'; |
4317 | 72 endif |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9659
diff
changeset
|
73 else |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
74 y = [a; b]; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
75 na = rows (a); nb = rows (b); |
559 | 76 endif |
77 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
78 if (nargout == 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
79 y = unique (y, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
80 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
81 [y, i] = unique (y, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
82 ia = i(i <= na); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
83 ib = i(i > na) - na; |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
84 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
85 |
559 | 86 endfunction |
7411 | 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; 4], [2, 3, 5]) == [1, 2, 3, 4, 5]))); | |
91 | |
92 %!assert(all (all (union ([1, 2, 3], [5; 7; 9]) == [1, 2, 3, 5, 7, 9]))); | |
93 | |
94 %!error union (1); | |
95 | |
96 %!error union (1, 2, 3); | |
97 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
98 %!test |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
99 %! 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
|
100 %! [y, ia, ib] = union (a, b.'); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
101 %! assert(y, [1, 2, 3, 4, 5]); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7411
diff
changeset
|
102 %! assert(y, sort([a(ia), b(ib)])); |