changeset 10372:634e182d34e4

unique.m: return 0x1 for empty matrices with some nonzero dims; preserve class of argument; new tests
author John W. Eaton <jwe@octave.org>
date Sun, 28 Feb 2010 11:41:03 -0500
parents dc8637fd7a76
children 1f11fabfa349
files scripts/ChangeLog scripts/set/unique.m
diffstat 2 files changed, 27 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Sun Feb 28 10:49:21 2010 -0500
+++ b/scripts/ChangeLog	Sun Feb 28 11:41:03 2010 -0500
@@ -1,3 +1,9 @@
+2010-02-28  John W. Eaton  <jwe@octave.org>
+
+	* set/unique.m: Return 0x1 arrays for empty arrays with some
+	nonzero dimensions.  Return object with the same class as the
+	argument.  New tests.
+
 2010-02-27  Liam Groener <liamg@mac.com>
 
 	* /time/datetick.m: Fix 'keepticks' bug, and untabify.
--- a/scripts/set/unique.m	Sun Feb 28 10:49:21 2010 -0500
+++ b/scripts/set/unique.m	Sun Feb 28 11:41:03 2010 -0500
@@ -54,7 +54,7 @@
       varargin = unique (varargin);
       optfirst = strmatch ("first", varargin) > 0;
       optlast = strmatch ("last", varargin) > 0;
-      optrows = strmatch ("rows", varargin) > 0 && size (x, 2) > 1;
+      optrows = strmatch ("rows", varargin) > 0;
       if (optfirst && optlast)
         error ("unique: cannot specify both \"last\" and \"first\"");
       elseif (optfirst + optlast + optrows != nargin-1)
@@ -84,6 +84,13 @@
 
   y = x;
   if (n < 1)
+    if (! optrows && isempty (x) && any (size (x)))
+      if (iscell (y))
+        y = cell (0, 1);
+      else
+        y = zeros (0, 1, class (y));
+      endif
+    endif
     i = j = [];
     return;
   elseif (n < 2)
@@ -137,7 +144,19 @@
 %!assert(unique([1,NaN,Inf,NaN,Inf]),[1,Inf,NaN,NaN])
 %!assert(unique({'Foo','Bar','Foo'}),{'Bar','Foo'})
 %!assert(unique({'Foo','Bar','FooBar'}'),{'Bar','Foo','FooBar'}')
-
+%!assert(unique(zeros(1,0)), zeros(0,1))
+%!assert(unique(zeros(1,0), 'rows'), zeros(1,0))
+%!assert(unique(cell(1,0)), cell(0,1))
+%!assert(unique({}), {})
+%!assert(unique([1,2,2,3,2,4], 'rows'), [1,2,2,3,2,4])
+%!assert(unique([1,2,2,3,2,4]), [1,2,3,4])
+%!assert(unique([1,2,2,3,2,4]', 'rows'), [1,2,3,4]')
+%!assert(unique(single([1,2,2,3,2,4]), 'rows'), single([1,2,2,3,2,4]))
+%!assert(unique(single([1,2,2,3,2,4])), single([1,2,3,4]))
+%!assert(unique(single([1,2,2,3,2,4]'), 'rows'), single([1,2,3,4]'))
+%!assert(unique(uint8([1,2,2,3,2,4]), 'rows'), uint8([1,2,2,3,2,4]))
+%!assert(unique(uint8([1,2,2,3,2,4])), uint8([1,2,3,4]))
+%!assert(unique(uint8([1,2,2,3,2,4]'), 'rows'), uint8([1,2,3,4]'))
 %!test
 %! [a,i,j] = unique([1,1,2,3,3,3,4]);
 %! assert(a,[1,2,3,4])