diff scripts/general/accumarray.m @ 10283:b178769f31ca

more small improvements in accumarray
author Jaroslav Hajek <highegg@gmail.com>
date Tue, 09 Feb 2010 10:48:31 +0100
parents 703038d648f1
children 6892c5562950
line wrap: on
line diff
--- a/scripts/general/accumarray.m
+++ b/scripts/general/accumarray.m
@@ -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