changeset 11923:304855b33b67 release-3-0-x

optimize unique called with a single argument
author Daniel J Sebald <daniel.sebald@ieee.org>
date Fri, 16 Jan 2009 08:10:57 +0100
parents 746f13936eee
children bacb84129896
files scripts/set/unique.m
diffstat 1 files changed, 22 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- 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