diff scripts/set/intersect.m @ 20009:72ccbd36e23c

Return a column vector by default for Matlab compatibility (bug #44425, bug #44453). Document that only unique elements are returned from set functions. * NEWS: Announce change in default orientation of return values. * intersect.m, setdiff.m, setxor.m, union.m: Return a column vector by default unless the input is explicitly a row vector. Change docstring to note that only unique, non-duplicative elements are returned.
author Rik <rik@octave.org>
date Fri, 20 Mar 2015 18:23:01 -0700
parents 4197fc428c7d
children c3b2ec6a1586
line wrap: on
line diff
--- a/scripts/set/intersect.m	Thu Mar 19 14:32:40 2015 -0400
+++ b/scripts/set/intersect.m	Fri Mar 20 18:23:01 2015 -0700
@@ -22,11 +22,11 @@
 ## @deftypefnx {Function File} {@var{c} =} intersect (@var{a}, @var{b}, "rows")
 ## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] =} intersect (@dots{})
 ##
-## Return the elements common to both @var{a} and @var{b} sorted in ascending
-## order.
+## Return the unique elements common to both @var{a} and @var{b} sorted in
+## ascending order.
 ##
-## If @var{a} and @var{b} are both column vectors then return a column vector;
-## Otherwise, return a row vector.  The inputs may also be cell arrays of
+## If @var{a} and @var{b} are both row vectors then return a row vector;
+## Otherwise, return a column vector.  The inputs may also be cell arrays of
 ## strings.
 ##
 ## If the optional input @qcode{"rows"} is given then return the common rows of
@@ -50,7 +50,7 @@
     c = ia = ib = [];
   else
     by_rows = nargin == 3;
-    iscol = isvector (a) && isvector (b) && iscolumn (a) && iscolumn (b);
+    isrowvec = isvector (a) && isvector (b) && isrow (a) && isrow (b);
 
     ## Form A and B into sets
     if (nargout > 1)
@@ -85,7 +85,7 @@
     endif
 
     ## Adjust output orientation for Matlab compatibility
-    if (! by_rows && ! iscol)
+    if (! by_rows && isrowvec)
       c = c.';
     endif
   endif
@@ -99,8 +99,8 @@
 %! b = 2:5;
 
 %!assert (size (intersect (a, b)), [1 3])
-%!assert (size (intersect (a', b)), [1 3])
-%!assert (size (intersect (a, b')), [1 3])
+%!assert (size (intersect (a', b)), [3 1])
+%!assert (size (intersect (a, b')), [3 1])
 %!assert (size (intersect (a', b')), [3 1])
 
 ## Test multi-dimensional arrays
@@ -108,7 +108,7 @@
 %! a = rand (3,3,3);
 %! b = a;
 %! b(1,1,1) = 2;
-%! assert (intersect (a, b), sort (a(2:end)));
+%! assert (intersect (a, b), sort (a(2:end)'));
 
 ## Test the routine for index vectors ia and ib
 %!test