# HG changeset patch # User Rik # Date 1562814364 25200 # Node ID 3d10834979f80c87e07757cb521e87337a883560 # Parent 9f44123dc25bced63d153e2fb5ad7c57f834b695 union.m: Accept a "legacy" flag for Matlab compatibility. * NEWS: Announce change. * union.m: Add new calling form and explanation of "legacy" option to docstring. Allow up to 4 inputs in input validation. Check for "legacy" in input options and set variable optlegacy. Set variable isrowvec based on optlegacy and orientation of inputs. Add BIST tests for "legacy" input. diff -r 9f44123dc25b -r 3d10834979f8 NEWS --- a/NEWS Wed Jul 10 19:51:37 2019 -0700 +++ b/NEWS Wed Jul 10 20:06:04 2019 -0700 @@ -53,9 +53,9 @@ behavior, or Matlab behavior from releases prior to R2012b, can be obtained by using the `"legacy"` flag. -- The function `intersect` now accepts a `"legacy"` flag which changes - the index values (second and third outputs) as well as the orientation - of the outputs to match Matlab releases prior to R2012b. +- The functions `intersect`, `union` now accept a `"legacy"` flag which + changes the index values (second and third outputs) as well as the + orientation of the outputs to match Matlab releases prior to R2012b. - Complex RESTful web services can now be accessed by the `webread` and `webwrite` functions alongside with the `weboptions` structure. One diff -r 9f44123dc25b -r 3d10834979f8 scripts/set/union.m --- a/scripts/set/union.m Wed Jul 10 19:51:37 2019 -0700 +++ b/scripts/set/union.m Wed Jul 10 20:06:04 2019 -0700 @@ -33,8 +33,8 @@ ## either @var{a} or @var{b}. The inputs must be 2-D matrices to use this ## option. ## -## The optional outputs @var{ia} and @var{ib} are index vectors such that -## @code{@var{a}(@var{ia})} and @code{@var{b}(@var{ib})} are disjoint sets +## The optional outputs @var{ia} and @var{ib} are column index vectors such +## that @code{@var{a}(@var{ia})} and @code{@var{b}(@var{ib})} are disjoint sets ## whose union is @var{c}. ## ## @seealso{unique, intersect, setdiff, setxor, ismember} @@ -44,14 +44,20 @@ function [y, ia, ib] = union (a, b, varargin) - if (nargin < 2 || nargin > 3) + if (nargin < 2 || nargin > 4) print_usage (); endif [a, b] = validsetargs ("union", a, b, varargin{:}); - by_rows = nargin == 3; - isrowvec = isrow (a) && isrow (b); + by_rows = any (strcmp ("rows", varargin)); + optlegacy = any (strcmp ("legacy", varargin)); + + if (optlegacy) + isrowvec = ! iscolumn (a) || ! iscolumn (b); + else + isrowvec = isrow (a) && isrow (b); + endif if (by_rows) y = [a; b]; @@ -104,6 +110,19 @@ %!assert (nthargout (2:3, @union, [1 2; 2 3; 4 5], [2 3; 3 4; 5 6], "rows"), %! {[1; 3], [1; 2; 3]}) +## Test "legacy" option +%!test +%! a = [5, 7, 1]; +%! b = [3, 1, 1]; +%! [c, ia, ib] = union (a,b); +%! assert (c, [1, 3, 5, 7]); +%! assert (ia, [3; 1; 2]); +%! assert (ib, [1]); +%! [c, ia, ib] = union (a,b, "legacy"); +%! assert (c, [1, 3, 5, 7]); +%! assert (ia, [1, 2]); +%! assert (ib, [3, 1]); + ## Test empty cell string array unions %!assert (union ({}, []), cell (0,1)) %!assert (union ([], {}), cell (0,1))