Mercurial > octave
diff scripts/general/accumarray.m @ 23493:bf063fafeca5
accumarray.m: check if func is a function handle
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Mon, 15 May 2017 15:58:57 +0100 |
parents | 092078913d54 |
children | b6144e6dda9e |
line wrap: on
line diff
--- a/scripts/general/accumarray.m Mon May 15 14:31:13 2017 +0100 +++ b/scripts/general/accumarray.m Mon May 15 15:58:57 2017 +0100 @@ -154,6 +154,12 @@ endif endif + if (isempty (func)) + func = @sum; + elseif (! is_function_handle (func)) + error ("accumarray: FUNC must be a function handle"); + endif + if (isempty (fillval)) fillval = 0; endif @@ -189,7 +195,7 @@ error ("accumarray: in the sparse case, values must be numeric or logical"); endif - if (! (isempty (func) || func == @sum)) + if (func != @sum) ## Reduce values. This is not needed if we're about to sum them, ## because "sparse" can do that. @@ -252,7 +258,7 @@ ## Some built-in reductions handled efficiently. - if (isempty (func) || func == @sum) + if (func == @sum) ## Fast summation. if (isempty (sz)) A = __accumarray_sum__ (subs, vals); @@ -446,3 +452,6 @@ ## 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)) + +%!error <FUNC must be a function handle> +%! accumarray ([1; 2; 3], [1; 2; 3], [3 1], '@(x) {x}')