# HG changeset patch # User Rik # Date 1439324695 25200 # Node ID 3fc21d7ac11ca54503af52463d1ae44c7aa65801 # Parent e59a44fa74ff447ed95d821fd9f97f40069edb00 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. diff -r e59a44fa74ff -r 3fc21d7ac11c scripts/set/intersect.m --- 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'], []), "")