changeset 20475:3fc21d7ac11c

intersect.m: Return same class of output as Matlab for empty input (bug #45686). * intersect.m: For an empty input, return cell, char, or matrix based on Matlab rules. Add BIST tests for behavior.
author Rik <rik@octave.org>
date Tue, 11 Aug 2015 13:24:55 -0700
parents e59a44fa74ff
children b22528fd3deb
files scripts/set/intersect.m
diffstat 1 files changed, 18 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/set/intersect.m	Mon Aug 03 23:22:14 2015 +0200
+++ b/scripts/set/intersect.m	Tue Aug 11 13:24:55 2015 -0700
@@ -47,7 +47,16 @@
   [a, b] = validsetargs ("intersect", a, b, varargin{:});
 
   if (isempty (a) || isempty (b))
-    c = ia = ib = [];
+    ## Special case shortcuts algorithm.
+    ## Lots of type checking required for Matlab compatibility.
+    if (isnumeric (a) && isnumeric (b))
+      c = [];
+    elseif (iscell (b))
+      c = {};
+    else
+      c = "";
+    endif
+    ia = ib = [];
   else
     by_rows = nargin == 3;
     isrowvec = isrow (a) && isrow (b);
@@ -141,3 +150,11 @@
 %! assert (ia, [1:3]');
 %! assert (ib, [1:3]');
 
+## Test return type of empty intersections
+%!assert (intersect (['a', 'b'], {}), {})
+%!assert (intersect ([], {'a', 'b'}), {})
+%!assert (intersect ([], {}), {})
+%!assert (intersect ({'a', 'b'}, []), {})
+%!assert (intersect ([], ['a', 'b']), "")
+%!assert (intersect ({}, []), {})
+%!assert (intersect (['a', 'b'], []), "")