Mercurial > octave-antonio
comparison scripts/set/setdiff.m @ 19974: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 | 9fc020886ae9 |
children | 03b9d17a2d95 |
comparison
equal
deleted
inserted
replaced
19973:7aaf756b1532 | 19974:72ccbd36e23c |
---|---|
19 | 19 |
20 ## -*- texinfo -*- | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {@var{c} =} setdiff (@var{a}, @var{b}) | 21 ## @deftypefn {Function File} {@var{c} =} setdiff (@var{a}, @var{b}) |
22 ## @deftypefnx {Function File} {@var{c} =} setdiff (@var{a}, @var{b}, "rows") | 22 ## @deftypefnx {Function File} {@var{c} =} setdiff (@var{a}, @var{b}, "rows") |
23 ## @deftypefnx {Function File} {[@var{c}, @var{ia}] =} setdiff (@dots{}) | 23 ## @deftypefnx {Function File} {[@var{c}, @var{ia}] =} setdiff (@dots{}) |
24 ## Return the elements in @var{a} that are not in @var{b} sorted in | 24 ## Return the unique elements in @var{a} that are not in @var{b} sorted in |
25 ## ascending order. | 25 ## ascending order. |
26 ## | 26 ## |
27 ## If @var{a} and @var{b} are both column vectors return a column vector; | 27 ## If @var{a} is a row vector return a column vector; |
28 ## Otherwise, return a row vector. The inputs may also be cell arrays of | 28 ## Otherwise, return a column vector. The inputs may also be cell arrays of |
29 ## strings. | 29 ## strings. |
30 ## | 30 ## |
31 ## If the optional input @qcode{"rows"} is given then return the rows in | 31 ## If the optional input @qcode{"rows"} is given then return the rows in |
32 ## @var{a} that are not in @var{b}. The inputs must be 2-D matrices to use | 32 ## @var{a} that are not in @var{b}. The inputs must be 2-D matrices to use |
33 ## this option. | 33 ## this option. |
47 endif | 47 endif |
48 | 48 |
49 [a, b] = validsetargs ("setdiff", a, b, varargin{:}); | 49 [a, b] = validsetargs ("setdiff", a, b, varargin{:}); |
50 | 50 |
51 by_rows = nargin == 3; | 51 by_rows = nargin == 3; |
52 iscol = isvector (a) && isvector (b) && iscolumn (a) && iscolumn (b); | 52 isrowvec = isvector (a) && isrow (a); |
53 | 53 |
54 if (by_rows) | 54 if (by_rows) |
55 if (nargout > 1) | 55 if (nargout > 1) |
56 [c, ia] = unique (a, "rows"); | 56 [c, ia] = unique (a, "rows"); |
57 else | 57 else |
87 c(idx(dups)) = []; | 87 c(idx(dups)) = []; |
88 if (nargout > 1) | 88 if (nargout > 1) |
89 ia(idx(dups)) = []; | 89 ia(idx(dups)) = []; |
90 endif | 90 endif |
91 ## Reshape if necessary for Matlab compatibility. | 91 ## Reshape if necessary for Matlab compatibility. |
92 if (iscol) | 92 if (isrowvec) |
93 c = c(:).'; | |
94 else | |
93 c = c(:); | 95 c = c(:); |
94 else | |
95 c = c(:).'; | |
96 endif | 96 endif |
97 endif | 97 endif |
98 endif | 98 endif |
99 | 99 |
100 endfunction | 100 endfunction |
118 ## Test output orientation compatibility (bug #42577) | 118 ## Test output orientation compatibility (bug #42577) |
119 %!assert (setdiff ([1:5], 2), [1,3,4,5]) | 119 %!assert (setdiff ([1:5], 2), [1,3,4,5]) |
120 %!assert (setdiff ([1:5]', 2), [1;3;4;5]) | 120 %!assert (setdiff ([1:5]', 2), [1;3;4;5]) |
121 %!assert (setdiff ([1:5], [2:3]), [1,4,5]) | 121 %!assert (setdiff ([1:5], [2:3]), [1,4,5]) |
122 %!assert (setdiff ([1:5], [2:3]'), [1,4,5]) | 122 %!assert (setdiff ([1:5], [2:3]'), [1,4,5]) |
123 %!assert (setdiff ([1:5]', [2:3]), [1,4,5]) | 123 %!assert (setdiff ([1:5]', [2:3]), [1;4;5]) |
124 %!assert (setdiff ([1:5]', [2:3]'), [1;4;5]) | 124 %!assert (setdiff ([1:5]', [2:3]'), [1;4;5]) |
125 | 125 |
126 %!test | 126 %!test |
127 %! a = rand (3,3,3); | 127 %! a = rand (3,3,3); |
128 %! b = a(1); | 128 %! b = a(1); |
129 %! assert (setdiff (a, b), sort (a(2:end))); | 129 %! assert (setdiff (a, b), sort (a(2:end)')); |
130 | 130 |