Mercurial > octave-nkf
comparison scripts/set/setxor.m @ 20009:72ccbd36e23c
Return a column vector by default for Matlab compatibility (bug #44425, bug #44453).
Document that only unique elements are returned from set functions.
* NEWS: Announce change in default orientation of return values.
* intersect.m, setdiff.m, setxor.m, union.m: Return a column vector by default
unless the input is explicitly a row vector. Change docstring to note that only
unique, non-duplicative elements are returned.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 20 Mar 2015 18:23:01 -0700 |
parents | 4197fc428c7d |
children | c3b2ec6a1586 |
comparison
equal
deleted
inserted
replaced
20008:7aaf756b1532 | 20009:72ccbd36e23c |
---|---|
21 ## -*- texinfo -*- | 21 ## -*- texinfo -*- |
22 ## @deftypefn {Function File} {@var{c} =} setxor (@var{a}, @var{b}) | 22 ## @deftypefn {Function File} {@var{c} =} setxor (@var{a}, @var{b}) |
23 ## @deftypefnx {Function File} {@var{c} =} setxor (@var{a}, @var{b}, "rows") | 23 ## @deftypefnx {Function File} {@var{c} =} setxor (@var{a}, @var{b}, "rows") |
24 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} setxor (@dots{}) | 24 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} setxor (@dots{}) |
25 ## | 25 ## |
26 ## Return the elements exclusive to sets @var{a} or @var{b} sorted in | 26 ## Return the unique elements exclusive to sets @var{a} or @var{b} sorted in |
27 ## ascending order. | 27 ## ascending order. |
28 ## | 28 ## |
29 ## If @var{a} and @var{b} are both column vectors return a column vector; | 29 ## If @var{a} and @var{b} are both row vectors then return a row vector; |
30 ## Otherwise, return a row vector. The inputs may also be cell arrays of | 30 ## Otherwise, return a column vector. The inputs may also be cell arrays of |
31 ## strings. | 31 ## strings. |
32 ## | 32 ## |
33 ## If the optional input @qcode{"rows"} is given then return the rows exclusive | 33 ## If the optional input @qcode{"rows"} is given then return the rows exclusive |
34 ## to sets @var{a} and @var{b}. The inputs must be 2-D matrices to use this | 34 ## to sets @var{a} and @var{b}. The inputs must be 2-D matrices to use this |
35 ## option. | 35 ## option. |
48 endif | 48 endif |
49 | 49 |
50 [a, b] = validsetargs ("setxor", a, b, varargin{:}); | 50 [a, b] = validsetargs ("setxor", a, b, varargin{:}); |
51 | 51 |
52 by_rows = nargin == 3; | 52 by_rows = nargin == 3; |
53 iscol = isvector (a) && isvector (b) && iscolumn (a) && iscolumn (b); | 53 isrowvec = isvector (a) && isvector (b) && isrow (a) && isrow (b); |
54 | 54 |
55 ## Form A and B into sets. | 55 ## Form A and B into sets. |
56 if (nargout > 1) | 56 if (nargout > 1) |
57 [a, ia] = unique (a, varargin{:}); | 57 [a, ia] = unique (a, varargin{:}); |
58 [b, ib] = unique (b, varargin{:}); | 58 [b, ib] = unique (b, varargin{:}); |
89 c([idx, idx+1]) = []; | 89 c([idx, idx+1]) = []; |
90 i([idx, idx+1]) = []; | 90 i([idx, idx+1]) = []; |
91 endif | 91 endif |
92 | 92 |
93 ## Adjust output orientation for Matlab compatibility | 93 ## Adjust output orientation for Matlab compatibility |
94 if (! iscol) | 94 if (isrowvec) |
95 c = c.'; | 95 c = c.'; |
96 endif | 96 endif |
97 endif | 97 endif |
98 | 98 |
99 if (nargout > 1) | 99 if (nargout > 1) |
110 | 110 |
111 %!test | 111 %!test |
112 %! a = [3, 1, 4, 1, 5]; | 112 %! a = [3, 1, 4, 1, 5]; |
113 %! b = [1, 2, 3, 4]; | 113 %! b = [1, 2, 3, 4]; |
114 %! [c, ia, ib] = setxor (a, b.'); | 114 %! [c, ia, ib] = setxor (a, b.'); |
115 %! assert (c, [2, 5]); | 115 %! assert (c, [2; 5]); |
116 %! assert (c, sort ([a(ia), b(ib)])); | 116 %! assert (c, sort ([a(ia)'; b(ib)'])); |
117 | 117 |
118 %!test | 118 %!test |
119 %! a = [1 2; 4 5; 1 3]; | 119 %! a = [1 2; 4 5; 1 3]; |
120 %! b = [1 1; 1 2; 4 5; 2 10]; | 120 %! b = [1 1; 1 2; 4 5; 2 10]; |
121 %! [c, ia, ib] = setxor (a, b, "rows"); | 121 %! [c, ia, ib] = setxor (a, b, "rows"); |
154 %!shared x,y | 154 %!shared x,y |
155 %! x = 1:3; | 155 %! x = 1:3; |
156 %! y = 2:5; | 156 %! y = 2:5; |
157 | 157 |
158 %!assert (size (setxor (x, y)), [1 3]) | 158 %!assert (size (setxor (x, y)), [1 3]) |
159 %!assert (size (setxor (x', y)), [1 3]) | 159 %!assert (size (setxor (x', y)), [3 1]) |
160 %!assert (size (setxor (x, y')), [1 3]) | 160 %!assert (size (setxor (x, y')), [3 1]) |
161 %!assert (size (setxor (x', y')), [3 1]) | 161 %!assert (size (setxor (x', y')), [3 1]) |
162 | 162 |
163 ## Test multi-dimensional arrays | 163 ## Test multi-dimensional arrays |
164 %!test | 164 %!test |
165 %! a = rand (3,3,3); | 165 %! a = rand (3,3,3); |
166 %! b = a; | 166 %! b = a; |
167 %! b(1,1,1) = 2; | 167 %! b(1,1,1) = 2; |
168 %! assert (intersect (a, b), sort (a(2:end))); | 168 %! assert (intersect (a, b), sort (a(2:end)')); |
169 | 169 |