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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 10793
diff changeset
2 ## Copyright (C) 2000, 2006-2007 Paul Kienzle
5825
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
3 ##
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
5 ##
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
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
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
9 ## your option) any later version.
5825
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
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
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
15 ##
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5825
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
19
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
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
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
24 ##
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
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
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
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
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
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
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
34 ## @seealso{unique, union, intersect, setdiff, ismember}
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
35 ## @end deftypefn
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
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
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5825
diff changeset
40 print_usage ();
5825
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
41 endif
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
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
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
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
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
53
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
54 if (isempty (a))
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
55 c = b;
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
56 elseif (isempty (b))
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
57 c = a;
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
58 else
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
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
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10088
diff changeset
66 c([idx, idx+1],:) = [];
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10088
diff changeset
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
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10088
diff changeset
79 c([idx, idx+1]) = [];
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10088
diff changeset
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
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10088
diff changeset
83 c = c.';
7920
e56bb65186f6 improve set functions for Matlab compatibility
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
84 endif
5825
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
85 endif
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
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
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
92 endfunction
f3e37beb03aa [project @ 2006-05-19 16:13:52 by jwe]
jwe
parents:
diff changeset
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