# HG changeset patch # User Daniel J Sebald # Date 1229629290 -3600 # Node ID 970b4dbff9e470b49897a4f3791dc8142823d123 # Parent 69d45a4c7d94c151bc33815c1260d216cc651173 optimize unique called with a single argument diff --git a/scripts/set/unique.m b/scripts/set/unique.m --- a/scripts/set/unique.m +++ b/scripts/set/unique.m @@ -45,26 +45,31 @@ print_usage (); endif - ## parse options - if (iscellstr (varargin)) - optfirst = strmatch ('first', varargin) > 0; - optlast = strmatch ('last', varargin) > 0; - optrows = strmatch ('rows', varargin) > 0 && size (x, 2) > 1; - if (optfirst && optlast) - error ("unique: cannot specify both 'last' and 'first'."); - elseif (optfirst + optlast + optrows != nargin-1) - error ("unique: invalid option."); + if (nargin > 1) + + ## parse options + if (iscellstr (varargin)) + varargin = unique(varargin); + optfirst = strmatch ('first', varargin) > 0; + optlast = strmatch ('last', varargin) > 0; + optrows = strmatch ('rows', varargin) > 0 && size (x, 2) > 1; + if (optfirst && optlast) + error ("unique: cannot specify both 'last' and 'first'."); + elseif (optfirst + optlast + optrows != nargin-1) + error ("unique: invalid option."); + endif + else + error ("unique: options must be strings"); endif - optlast = ! optfirst; - else - error ("unique: options must be strings"); - endif - if (iscell (x)) - if (optrows) + if (optrows && iscell (x)) warning ("unique: 'rows' is ignored for cell arrays"); optrows = false; endif + + else + optfirst = 0; + optrows = 0; endif if (optrows) @@ -101,12 +106,11 @@ y(idx) = []; endif - ## I don't know why anyone would need reverse indices, but it - ## was an interesting challenge. I welcome cleaner solutions. if (nargout >= 3) j = i; - j(i) = cumsum (prepad (! match, n, 1)); + j(i) = cumsum ([1 !match]); endif + if (optfirst) i(idx+1) = []; else