changeset 13787:8bb7bdbe9c69

Clarify max/min in accumarray and uniformise its behaviour
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Wed, 02 Nov 2011 00:25:27 -0400
parents 40dab5d70115
children 9ab64f063c96
files scripts/general/accumarray.m
diffstat 1 files changed, 11 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/accumarray.m	Tue Nov 01 23:18:08 2011 -0400
+++ b/scripts/general/accumarray.m	Wed Nov 02 00:25:27 2011 -0400
@@ -40,8 +40,14 @@
 ## function should not depend on the order of the subscripts.
 ##
 ## The elements of the returned array that have no subscripts associated
-## with them are set to zero.  Defining @var{fillval} to some other
-## value allows these values to be defined.
+## with them are set to zero. Defining @var{fillval} to some other value
+## allows these values to be defined. This behaviour changes, however,
+## for certain values of @var{func}. If @var{func} is @code{min}
+## (respectively, @code{max}) then the result will be filled with the
+## minimum (respectively, maximum) integer if @var{vals} is of integral
+## type, logical false (respectively, logical true) if @var{vals} is of
+## logical type, zero if @var{fillval} is zero and all values are
+## nonpositive (respectively, nonnegative), and NaN otherwise.
 ##
 ## By default @code{accumarray} returns a full matrix.  If
 ## @var{issparse} is logically true, then a sparse matrix is returned
@@ -248,6 +254,9 @@
         zero = intmax (class (vals));
       elseif (islogical (vals))
         zero = true;
+      elseif (fillval == 0 && all (vals(:) <= 0))
+        ## This is a common case - fillval is zero, all numbers nonpositive.
+        zero = 0;
       else
         zero = NaN; # Neutral value.
       endif