# HG changeset patch # User John W. Eaton # Date 1267378336 18000 # Node ID 1f11fabfa34957118b2a7c3a118f2e9e1c8cbd51 # Parent 634e182d34e461e091c60dab5e54a77d43582370 improve performance of unique for sparse arrays diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,10 @@ +2010-02-28 John W. Eaton + + * set/unique.m: If the argument is sparse and we are not + operating on rows and we don't need indices, convert nonzero + elements to a full matrix and work on that instead, converting + back to sparse when done. + 2010-02-28 John W. Eaton * set/unique.m: Return 0x1 arrays for empty arrays with some diff --git a/scripts/set/unique.m b/scripts/set/unique.m --- a/scripts/set/unique.m +++ b/scripts/set/unique.m @@ -74,6 +74,26 @@ optrows = 0; endif + ## FIXME -- the operations + ## + ## match = (y(1:n-1) == y(2:n)); + ## y(idx) = []; + ## + ## are very slow on sparse matrices. Until they are fixed to be as + ## fast as for full matrices, operate on the nonzero elements of the + ## sparse array as long as we are not operating on rows. + + ## FIXME -- unique is called when PKG_ADD files are parsed, but + ## issparse is not yet available because it is coming from a .oct + ## file?!? + + if (exist ("issparse")) + if (issparse (x) && ! optrows && nargout <= 1) + y = unique ([0; (full (nonzeros (x)))], varargin{:}); + return; + endif + endif + if (optrows) n = size (x, 1); dim = 1; @@ -132,7 +152,6 @@ i(idx) = []; endif - endfunction %!assert(unique([1 1 2; 1 2 1; 1 1 2]),[1;2])