Mercurial > octave-dspies
diff scripts/set/setxor.m @ 19003:d00f6b09258f @
Overhaul functions in scripts/set directory.
* set.txi: Rewrite documentation for set functions.
* intersect.m: Rewrite docstring. Use by_rows variable for code clarity.
Return output orientation which is compatible with Matlab. Add %!tests for
output orientation and N-dimensional inputs.
* setdiff.m: Rewrite docstring. Use by_rows variable for code clarity.
Rename output i to ia to clarify it is an index into the set a.
Return output orientation which is compatible with Matlab. Add %!tests for
N-dimensional inputs.
* setxor.m: Rewrite docstring. Use by_rows variable for code clarity.
Return output orientation which is compatible with Matlab. Add %!tests for
output orientation and N-dimensional inputs.
* union.m: Rewrite docstring. Use by_rows variable for code clarity.
Return output orientation which is compatible with Matlab. Add %!tests for
output orientation and N-dimensional inputs. Add %!tests for validsetargs
which are common to all set functions.
* unique.m: Rewrite docstring. Verify that input is numeric or cell array
of strings. Avoid computing idx for optional i,j outputs unless required.
Add %!error tests for input validation.
* ismember.m: Rewrite docstring. Use input variable 'a' instead of 'A' for
conformance with rest of set functions. Rename output index variable to
s_idx for clarity that it is an index into the set s.
* powerset.m: Rewrite doctring. Add input validation on nargin. Add %!error
input validation tests.
* module.mk: Include validsetargs.m in build system.
* validsetargs.m: Function renamed from validargs which was too general.
* validargs.m: Function renamed to validsetargs.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 11 Aug 2014 09:39:45 -0700 |
parents | 42df174ec2ff |
children |
line wrap: on
line diff
--- a/scripts/set/setxor.m Sun Aug 10 08:18:18 2014 -0700 +++ b/scripts/set/setxor.m Mon Aug 11 09:39:45 2014 -0700 @@ -18,18 +18,24 @@ ## <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} {} setxor (@var{a}, @var{b}) -## @deftypefnx {Function File} {} setxor (@var{a}, @var{b}, "rows") -## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} setxor (@var{a}, @var{b}) +## @deftypefn {Function File} {@var{c} =} setxor (@var{a}, @var{b}) +## @deftypefnx {Function File} {@var{c} =} setxor (@var{a}, @var{b}, "rows") +## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} setxor (@dots{}) +## +## Return the elements exclusive to sets @var{a} or @var{b} sorted in +## ascending order. ## -## Return the elements exclusive to @var{a} or @var{b}, sorted in ascending -## order. If @var{a} and @var{b} are both column vectors return a column -## vector, otherwise return a row vector. -## @var{a}, @var{b} may be cell arrays of string(s). +## If @var{a} and @var{b} are both column vectors return a column vector; +## Otherwise, return a row vector. The inputs may also be cell arrays of +## strings. ## -## With three output arguments, return index vectors @var{ia} and @var{ib} -## such that @code{a(ia)} and @code{b(ib)} are disjoint sets whose union -## is @var{c}. +## If the optional input @qcode{"rows"} is given then return the rows exclusive +## to sets @var{a} and @var{b}. The inputs must be 2-D matrices to use this +## option. +## +## If requested, return index vectors @var{ia} and @var{ib} such that +## @code{@var{a}(@var{ia})} and @code{@var{b}(@var{ib})} are disjoint sets +## whose union is @var{c}. ## ## @seealso{unique, union, intersect, setdiff, ismember} ## @end deftypefn @@ -40,7 +46,10 @@ print_usage (); endif - [a, b] = validargs ("setxor", a, b, varargin{:}); + [a, b] = validsetargs ("setxor", a, b, varargin{:}); + + by_rows = nargin == 3; + iscol = isvector (a) && isvector (b) && iscolumn (a) && iscolumn (b); ## Form A and B into sets. if (nargout > 1) @@ -57,8 +66,8 @@ c = a; else ## Reject duplicates. - if (nargin > 2) - na = rows (a); nb = rows (b); + if (by_rows) + na = rows (a); nb = rows (b); [c, i] = sortrows ([a; b]); n = rows (c); idx = find (all (c(1:n-1, :) == c(2:n, :), 2)); @@ -67,7 +76,7 @@ i([idx, idx+1],:) = []; endif else - na = numel (a); nb = numel (b); + na = numel (a); nb = numel (b); [c, i] = sort ([a(:); b(:)]); n = length (c); if (iscell (c)) @@ -79,11 +88,14 @@ c([idx, idx+1]) = []; i([idx, idx+1]) = []; endif - if (rows (a) == 1 || rows (b) == 1) + + ## Adjust output orientation for Matlab compatibility + if (! iscol) c = c.'; endif endif endif + if (nargout > 1) ia = ia(i(i <= na)); ib = ib(i(i > na) - na); @@ -92,18 +104,37 @@ endfunction -%!assert (setxor ([1,2,3],[2,3,4]),[1,4]) +%!assert (setxor ([1,2,3], [2,3,4]), [1,4]) %!assert (setxor ({'a'}, {'a', 'b'}), {'b'}) %!test -%! a = [3, 1, 4, 1, 5]; b = [1, 2, 3, 4]; +%! a = [3, 1, 4, 1, 5]; +%! b = [1, 2, 3, 4]; %! [c, ia, ib] = setxor (a, b.'); %! assert (c, [2, 5]); %! assert (c, sort ([a(ia), b(ib)])); %!test -%! a = [1 2; 4 5; 1 3]; b = [1 1; 1 2; 4 5; 2 10]; +%! a = [1 2; 4 5; 1 3]; +%! b = [1 1; 1 2; 4 5; 2 10]; %! [c, ia, ib] = setxor (a, b, "rows"); %! assert (c, [1 1; 1 3; 2 10]); %! assert (c, sortrows ([a(ia,:); b(ib,:)])); +## Test orientation of output +%!shared x,y +%! x = 1:3; +%! y = 2:5; + +%!assert (size (setxor (x, y)), [1 3]) +%!assert (size (setxor (x', y)), [1 3]) +%!assert (size (setxor (x, y')), [1 3]) +%!assert (size (setxor (x', y')), [3 1]) + +## Test multi-dimensional arrays +%!test +%! a = rand (3,3,3); +%! b = a; +%! b(1,1,1) = 2; +%! assert (intersect (a, b), sort (a(2:end))); +