changeset 20448:2edd668e7784

union.m: Matlab compatible output orientation for empty input ([]). * union.m: Check inputs a,b with isempty which qualifies as a row vector. Add BIST tests to verify new behavior.
author Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
date Fri, 31 Jul 2015 14:01:39 +0200
parents 110c7a54586b
children 046904b54dc4
files scripts/set/union.m
diffstat 1 files changed, 7 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/set/union.m	Wed Jul 29 21:16:13 2015 -0700
+++ b/scripts/set/union.m	Fri Jul 31 14:01:39 2015 +0200
@@ -51,7 +51,7 @@
   [a, b] = validsetargs ("union", a, b, varargin{:});
 
   by_rows = nargin == 3;
-  isrowvec = isvector (a) && isvector (b) && isrow (a) && isrow (b);
+  isrowvec = (isrow (a) || isempty (a)) && (isrow (b) || isempty (b));
 
   if (by_rows)
     y = [a; b];
@@ -94,6 +94,12 @@
 %! assert (y, [1; 2; 3; 4; 5]);
 %! assert (y, sort ([a(ia)'; b(ib)']));
 
+## Test format when input is empty
+%!assert (union ([],[1,2]), [1,2])
+%!assert (union ([1,2],[]), [1,2])
+%!assert (union ([],[1;2]), [1;2])
+%!assert (union ([1;2],[]), [1;2])
+
 ## Test common input validation for set routines contained in validsetargs
 %!error <cell array of strings cannot be combined> union ({"a"}, 1)
 %!error <A and B must be arrays or cell arrays> union (@sin, 1)
@@ -102,4 +108,3 @@
 %!error <A and B must be arrays or cell arrays> union (@sin, 1, "rows")
 %!error <A and B must be 2-dimensional matrices> union (rand(2,2,2), 1, "rows")
 %!error <number of columns in A and B must match> union ([1 2], 1, "rows")
-