comparison scripts/set/intersect.m @ 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 c3b2ec6a1586
children
comparison
equal deleted inserted replaced
20473:e59a44fa74ff 20475:3fc21d7ac11c
45 endif 45 endif
46 46
47 [a, b] = validsetargs ("intersect", a, b, varargin{:}); 47 [a, b] = validsetargs ("intersect", a, b, varargin{:});
48 48
49 if (isempty (a) || isempty (b)) 49 if (isempty (a) || isempty (b))
50 c = ia = ib = []; 50 ## Special case shortcuts algorithm.
51 ## Lots of type checking required for Matlab compatibility.
52 if (isnumeric (a) && isnumeric (b))
53 c = [];
54 elseif (iscell (b))
55 c = {};
56 else
57 c = "";
58 endif
59 ia = ib = [];
51 else 60 else
52 by_rows = nargin == 3; 61 by_rows = nargin == 3;
53 isrowvec = isrow (a) && isrow (b); 62 isrowvec = isrow (a) && isrow (b);
54 63
55 ## Form A and B into sets 64 ## Form A and B into sets
139 %! [b, ia, ib] = intersect (a, a, "rows"); 148 %! [b, ia, ib] = intersect (a, a, "rows");
140 %! assert (b, a); 149 %! assert (b, a);
141 %! assert (ia, [1:3]'); 150 %! assert (ia, [1:3]');
142 %! assert (ib, [1:3]'); 151 %! assert (ib, [1:3]');
143 152
153 ## Test return type of empty intersections
154 %!assert (intersect (['a', 'b'], {}), {})
155 %!assert (intersect ([], {'a', 'b'}), {})
156 %!assert (intersect ([], {}), {})
157 %!assert (intersect ({'a', 'b'}, []), {})
158 %!assert (intersect ([], ['a', 'b']), "")
159 %!assert (intersect ({}, []), {})
160 %!assert (intersect (['a', 'b'], []), "")