changeset 10283:b178769f31ca

more small improvements in accumarray
author Jaroslav Hajek <highegg@gmail.com>
date Tue, 09 Feb 2010 10:48:31 +0100
parents c9780d8e228c
children c3df189b1b15
files scripts/ChangeLog scripts/general/accumarray.m
diffstat 2 files changed, 11 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Tue Feb 09 09:44:19 2010 +0100
+++ b/scripts/ChangeLog	Tue Feb 09 10:48:31 2010 +0100
@@ -1,3 +1,8 @@
+2010-02-09  Jaroslav Hajek  <highegg@gmail.com>
+
+	* general/accumarray.m: Use != instead of diff to compare indices in
+	dense case. Set explicit mode in sparse case to aid optimization.
+
 2010-02-09  Jaroslav Hajek  <highegg@gmail.com>
 
 	* miscellaneous/menu.m: Use sscanf rather than eval.
--- a/scripts/general/accumarray.m	Tue Feb 09 09:44:19 2010 +0100
+++ b/scripts/general/accumarray.m	Tue Feb 09 10:48:31 2010 +0100
@@ -126,13 +126,16 @@
 
       val = cellfun (func, mat2cell (val(:)(idx), diff ([0; jdx])));
       subs = subs(jdx, :);
+      mode = "unique";
+    else
+      mode = "sum";
     endif
 
     ## Form the sparse matrix.
     if (isempty (sz))
-      A = sparse (subs(:,1), subs(:,2), val);
+      A = sparse (subs(:,1), subs(:,2), val, mode);
     elseif (length (sz) == 2)
-      A = sparse (subs(:,1), subs(:,2), val, sz(1), sz(2));
+      A = sparse (subs(:,1), subs(:,2), val, sz(1), sz(2), mode);
     else
       error ("accumarray: dimensions mismatch")
     endif
@@ -243,7 +246,7 @@
       ## Sort indices.
       [subs, idx] = sort (subs);
       ## Identify runs.
-      jdx = find (diff (subs, 1, 1));
+      jdx = find (subs(1:n-1) != subs(2:n));
       jdx = [jdx; n];
       val = mat2cell (val(idx), diff ([0; jdx]));
       ## Optimize the case when function is @(x) {x}, i.e. we just want to