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 02:10:03 -0600
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.3) Gecko/20041020

Daniel J Sebald wrote:
Jaroslav,

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

Use this one instead.  I had the dimension wrong on the 'rows' case.  I also 
added the test

A=[1,2,3;1,2,3];
[a,i,j] = unique(A,'rows');
assert(a,[1,2,3])
assert(A(i,:),a)
assert(a(j,:),A)

Thank you,

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.000000000 -0600
+++ octave_mod/scripts/set/unique.m     2009-01-12 02:03:40.000000000 -0600
@@ -74,8 +74,10 @@
 
   if (optrows)
     n = size (x, 1);
+    dim = 1;
   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,20 @@
 %! 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]')
+%!
+%!test
+%! A=[1,2,3;1,2,3];
+%! [a,i,j] = unique(A,'rows');
+%! assert(a,[1,2,3])
+%! assert(A(i,:),a)
+%! assert(a(j,:),A)

reply via email to

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