Mercurial > octave
changeset 21401:2f402f7c1a0b
accumarray: fix general case for empty list of subscripts (bug #47287)
* accumarray.m: when SUBS is an empty array, accumarray was working only
for the special cases of max, min, and sum. Fix that. Add tests.
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Sun, 28 Feb 2016 18:21:07 +0000 |
parents | 00960129ebf4 |
children | 8cfc50637511 |
files | scripts/general/accumarray.m |
diffstat | 1 files changed, 17 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/general/accumarray.m Sun Feb 28 16:55:41 2016 +0000 +++ b/scripts/general/accumarray.m Sun Feb 28 18:21:07 2016 +0000 @@ -336,7 +336,9 @@ [subs, idx] = sort (subs); ## Identify runs. jdx = find (subs(1:n-1) != subs(2:n)); - jdx = [jdx; n]; + if (n != 0) # bug #47287 + jdx = [jdx; n]; + endif vals = mat2cell (vals(idx), diff ([0; jdx])); ## Optimize the case when function is @(x) {x}, i.e. we just want ## to collect the values to cells. @@ -344,6 +346,7 @@ if (! strcmp (func2str (func), simple_cell_str)) vals = cellfun (func, vals); endif + subs = subs(jdx); if (isempty (sz)) @@ -427,3 +430,16 @@ %!error (accumarray (1:5)) %!error (accumarray ([1,2,3],1:2)) + +## Handle empty arrays (bug #47287) +%!test +%! ## min, max, and sum are special cases within accumarray so test them. +%! funcs = {@(x) length (x) > 1, @min, @max, @sum}; +%! for idx = 1:numel (funcs) +%! assert (accumarray (zeros (0, 1), [], [0 1] , funcs{idx}), zeros (0, 1)) +%! assert (accumarray (zeros (0, 1), [], [1 0] , funcs{idx}), zeros (1, 0)) +%! endfor + +## Matlab returns an array of doubles even though FUNC returns cells. In +## Octave, we do not have that bug, at least for this case. +%!assert (accumarray (zeros (0, 1), [], [0 1] , @(x) {x}), cell (0, 1))