# HG changeset patch # User John W. Eaton # Date 1267375263 18000 # Node ID 634e182d34e461e091c60dab5e54a77d43582370 # Parent dc8637fd7a76096fb6aaef67c0708b11a9cd5474 unique.m: return 0x1 for empty matrices with some nonzero dims; preserve class of argument; new tests diff -r dc8637fd7a76 -r 634e182d34e4 scripts/ChangeLog --- 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 + + * 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 * /time/datetick.m: Fix 'keepticks' bug, and untabify. diff -r dc8637fd7a76 -r 634e182d34e4 scripts/set/unique.m --- 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])