# HG changeset patch # User Daniel J Sebald # Date 1232089857 -3600 # Node ID 304855b33b67af52e6d4049167d22e29faebd21f # Parent 746f13936eeec0d7e376012a00a98305f1e75384 optimize unique called with a single argument diff -r 746f13936eee -r 304855b33b67 scripts/set/unique.m --- a/scripts/set/unique.m Fri Jan 16 08:10:28 2009 +0100 +++ b/scripts/set/unique.m Fri Jan 16 08:10:57 2009 +0100 @@ -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