diff scripts/general/accumarray.m @ 14113:dac62c415e8b stable

Do more error checking on accumarray and accumdim input. * accumarray.m: Check that number indices matches number of values to accumulate. Document this change. * accumdim.m: Check that length of index vector equals length of array along given dimension. Also let the extension parameter be empty in addition to being zero. Document this change and reformat docstring. * data.cc (do_accumdim_sum): Check that number of indices matches number of values.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 27 Dec 2011 13:45:30 -0500
parents 5b49cafe0599
children da67f032a712
line wrap: on
line diff
--- a/scripts/general/accumarray.m	Mon Dec 26 10:17:47 2011 -0500
+++ b/scripts/general/accumarray.m	Tue Dec 27 13:45:30 2011 -0500
@@ -26,7 +26,9 @@
 ## the rows of the matrix @var{subs} and the values by @var{vals}.  Each
 ## row of @var{subs} corresponds to one of the values in @var{vals}.  If
 ## @var{vals} is a scalar, it will be used for each of the row of
-## @var{subs}.
+## @var{subs}. If @var{subs} is a cell array of vectors, all vectors
+## must be of the same length, and the subscripts in the @var{k}th
+## vector must correspond to the @var{k}th dimension of the result.
 ##
 ## The size of the matrix will be determined by the subscripts
 ## themselves.  However, if @var{sz} is defined it determines the matrix
@@ -107,14 +109,26 @@
     print_usage ();
   endif
 
+  lenvals = length (vals);
+
   if (iscell (subs))
-    subs = cellfun ("vec", subs, "uniformoutput", false);
+    subs = cellfun (@vec, subs, "uniformoutput", false);
     ndims = numel (subs);
     if (ndims == 1)
       subs = subs{1};
     endif
+
+    lensubs = cellfun (@length, subs);
+
+    if (any (lensubs != lensubs(1)) || (lenvals > 1 && lenvals != lensubs(1)))
+      error ("accumarray: dimension mismatch");
+    endif
+
   else
     ndims = columns (subs);
+    if (lenvals > 1 && lenvals != rows (subs))
+      error ("accumarray: dimension mismatch")
+    endif
   endif
 
   if (isempty (fillval))