Mercurial > octave-nkf
annotate scripts/set/setxor.m @ 18081:42df174ec2ff stable
setxor.m: Fix bug when "rows" argument given (bug #40808).
* setxor.m: When "rows" argument given, use ':' to pick up all columns in find.
Add %!test to check behavior.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 04 Dec 2013 14:51:45 -0800 |
parents | d63878346099 |
children | d00f6b09258f |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
14872
diff
changeset
|
1 ## Copyright (C) 2008-2013 Jaroslav Hajek |
11523 | 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 | |
12849
e0a58c741996
codesprint: Reformat setxor's docstring
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11587
diff
changeset
|
7 ## under the terms of the GNU General Public License as published by the |
e0a58c741996
codesprint: Reformat setxor's docstring
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11587
diff
changeset
|
8 ## Free Software Foundation; either version 3 of the License, or (at |
7016 | 9 ## your option) any later version. |
5825 | 10 ## |
12849
e0a58c741996
codesprint: Reformat setxor's docstring
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11587
diff
changeset
|
11 ## Octave is distributed in the hope that it will be useful, but WITHOUT |
e0a58c741996
codesprint: Reformat setxor's docstring
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11587
diff
changeset
|
12 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
e0a58c741996
codesprint: Reformat setxor's docstring
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11587
diff
changeset
|
13 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
e0a58c741996
codesprint: Reformat setxor's docstring
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11587
diff
changeset
|
14 ## for more details. |
5825 | 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}) |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
22 ## @deftypefnx {Function File} {} setxor (@var{a}, @var{b}, "rows") |
12849
e0a58c741996
codesprint: Reformat setxor's docstring
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11587
diff
changeset
|
23 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} setxor (@var{a}, @var{b}) |
5825 | 24 ## |
25 ## 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
|
26 ## order. If @var{a} and @var{b} are both column vectors return a column |
5825 | 27 ## 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
|
28 ## @var{a}, @var{b} may be cell arrays of string(s). |
5825 | 29 ## |
12849
e0a58c741996
codesprint: Reformat setxor's docstring
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11587
diff
changeset
|
30 ## With three output arguments, return index vectors @var{ia} and @var{ib} |
e0a58c741996
codesprint: Reformat setxor's docstring
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11587
diff
changeset
|
31 ## such that @code{a(ia)} and @code{b(ib)} are disjoint sets whose union |
e0a58c741996
codesprint: Reformat setxor's docstring
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11587
diff
changeset
|
32 ## is @var{c}. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
33 ## |
5825 | 34 ## @seealso{unique, union, intersect, setdiff, ismember} |
35 ## @end deftypefn | |
36 | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
37 function [c, ia, ib] = setxor (a, b, varargin) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
38 |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
39 if (nargin < 2 || nargin > 3) |
6046 | 40 print_usage (); |
5825 | 41 endif |
42 | |
10088
5edee330d4cb
better argument checking and handling in set functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9659
diff
changeset
|
43 [a, b] = validargs ("setxor", a, b, varargin{:}); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
44 |
5825 | 45 ## Form A and B into sets. |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
46 if (nargout > 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
47 [a, ia] = unique (a, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
48 [b, ib] = unique (b, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
49 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
50 a = unique (a, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
51 b = unique (b, varargin{:}); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
52 endif |
5825 | 53 |
54 if (isempty (a)) | |
55 c = b; | |
56 elseif (isempty (b)) | |
57 c = a; | |
58 else | |
59 ## Reject duplicates. | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
60 if (nargin > 2) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
61 na = rows (a); nb = rows (b); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
62 [c, i] = sortrows ([a; b]); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
63 n = rows (c); |
18081
42df174ec2ff
setxor.m: Fix bug when "rows" argument given (bug #40808).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
64 idx = find (all (c(1:n-1, :) == c(2:n, :), 2)); |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
65 if (! isempty (idx)) |
10549 | 66 c([idx, idx+1],:) = []; |
67 i([idx, idx+1],:) = []; | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
68 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
69 else |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
70 na = numel (a); nb = numel (b); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
71 [c, i] = sort ([a(:); b(:)]); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
72 n = length (c); |
9481
a3ae7abaf659
support cellstrs in setxor
Pieter Eendebak <pieter.eendebak@gmail.com>
parents:
9051
diff
changeset
|
73 if (iscell (c)) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
74 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
|
75 else |
a3ae7abaf659
support cellstrs in setxor
Pieter Eendebak <pieter.eendebak@gmail.com>
parents:
9051
diff
changeset
|
76 idx = find (c(1:n-1) == c(2:n)); |
a3ae7abaf659
support cellstrs in setxor
Pieter Eendebak <pieter.eendebak@gmail.com>
parents:
9051
diff
changeset
|
77 endif |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
78 if (! isempty (idx)) |
10549 | 79 c([idx, idx+1]) = []; |
80 i([idx, idx+1]) = []; | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
81 endif |
14872
c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents:
14868
diff
changeset
|
82 if (rows (a) == 1 || rows (b) == 1) |
10549 | 83 c = c.'; |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
84 endif |
5825 | 85 endif |
86 endif | |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
87 if (nargout > 1) |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
88 ia = ia(i(i <= na)); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
89 ib = ib(i(i > na) - na); |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
90 endif |
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
91 |
5825 | 92 endfunction |
93 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
94 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
95 %!assert (setxor ([1,2,3],[2,3,4]),[1,4]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
96 %!assert (setxor ({'a'}, {'a', 'b'}), {'b'}) |
18081
42df174ec2ff
setxor.m: Fix bug when "rows" argument given (bug #40808).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
97 |
7920
e56bb65186f6
improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
98 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
99 %! a = [3, 1, 4, 1, 5]; b = [1, 2, 3, 4]; |
18081
42df174ec2ff
setxor.m: Fix bug when "rows" argument given (bug #40808).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
100 %! [c, ia, ib] = setxor (a, b.'); |
42df174ec2ff
setxor.m: Fix bug when "rows" argument given (bug #40808).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
101 %! assert (c, [2, 5]); |
42df174ec2ff
setxor.m: Fix bug when "rows" argument given (bug #40808).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
102 %! assert (c, sort ([a(ia), b(ib)])); |
9498
f7cc8f30f3b8
Added test for setxor
Pieter Eendebak <pieter.eendebak@gmail.com>
parents:
9481
diff
changeset
|
103 |
18081
42df174ec2ff
setxor.m: Fix bug when "rows" argument given (bug #40808).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
104 %!test |
42df174ec2ff
setxor.m: Fix bug when "rows" argument given (bug #40808).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
105 %! a = [1 2; 4 5; 1 3]; b = [1 1; 1 2; 4 5; 2 10]; |
42df174ec2ff
setxor.m: Fix bug when "rows" argument given (bug #40808).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
106 %! [c, ia, ib] = setxor (a, b, "rows"); |
42df174ec2ff
setxor.m: Fix bug when "rows" argument given (bug #40808).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
107 %! assert (c, [1 1; 1 3; 2 10]); |
42df174ec2ff
setxor.m: Fix bug when "rows" argument given (bug #40808).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
108 %! assert (c, sortrows ([a(ia,:); b(ib,:)])); |
42df174ec2ff
setxor.m: Fix bug when "rows" argument given (bug #40808).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
109 |