octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Speed up 'unique'


From: Daniel J Sebald
Subject: Re: Speed up 'unique'
Date: Mon, 12 Jan 2009 01:38:25 -0600
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.3) Gecko/20041020

Jaroslav,

There are some additional bugs in unique.m.  Could you please apply the 
attached patch as well?

One issue is that 'match' can be vertical, hence two different types of 
concatenation are required.  However, it can be the case that 'match' is a 
single scalar in some combinations of number of elements and uniqueness of 
those elements.  Hence a dimension 'dim' must be kept track of rather than 
relying on 'y' or 'match'.

Thanks,

Dan
diff -Pur octave/scripts/set/unique.m octave_mod/scripts/set/unique.m
--- octave/scripts/set/unique.m 2009-01-12 01:04:35.945116000 -0600
+++ octave_mod/scripts/set/unique.m     2009-01-12 01:15:59.743984000 -0600
@@ -74,8 +74,10 @@
 
   if (optrows)
     n = size (x, 1);
+    dim = 2;
   else
     n = numel (x);
+    dim = (size (x, 1) == 1) + 1;
   endif
 
   y = x;
@@ -108,7 +110,11 @@
 
   if (nargout >= 3)
     j = i;
-    j(i) = cumsum ([1, !match]);
+    if (dim == 1)
+      j(i) = cumsum ([1; !match]);
+    else
+      j(i) = cumsum ([1, !match]);
+    end
   endif
 
   if (optfirst)
@@ -128,7 +134,7 @@
 %!assert(unique([1;2]),[1;2])
 %!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({'Foo','Bar','FooBar'}'),{'Bar','Foo','FooBar'}')
 
 %!test
 %! [a,i,j] = unique([1,1,2,3,3,3,4]);
@@ -137,7 +143,13 @@
 %! assert(j,[1,1,2,3,3,3,4])
 %!
 %!test
-%! [a,i,j] = unique([1,1,2,3,3,3,4],'first');
-%! assert(a,[1,2,3,4])
-%! assert(i,[1,3,4,7])
-%! assert(j,[1,1,2,3,3,3,4])
+%! [a,i,j] = unique([1,1,2,3,3,3,4]','first');
+%! assert(a,[1,2,3,4]')
+%! assert(i,[1,3,4,7]')
+%! assert(j,[1,1,2,3,3,3,4]')
+%!
+%!test
+%! [a,i,j] = unique({'z'; 'z'; 'z'});
+%! assert(a,{'z'})
+%! assert(i,[3]')
+%! assert(j,[1,1,1]')

reply via email to

[Prev in Thread] Current Thread [Next in Thread]