changeset 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 cc5e92e3182b
children b9c66b9b5fc8
files scripts/general/accumarray.m scripts/image/gray2ind.m
diffstat 2 files changed, 21 insertions(+), 5 deletions(-) [+]
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}')
--- a/scripts/image/gray2ind.m	Mon May 15 14:31:13 2017 +0100
+++ b/scripts/image/gray2ind.m	Mon May 15 15:58:57 2017 +0100
@@ -43,8 +43,8 @@
     print_usage ();
   elseif (! isreal (I) || issparse (I) || ! ismatrix(I))
     error ("gray2ind: I must be a grayscale or binary image");
-  elseif (! isscalar (n) || n < 1 || n > 65536)
-    error ("gray2ind: N must be a positive integer in the range [1, 65536]");
+  elseif (! isscalar (n) || n < 1 )
+    error ("gray2ind: N must be a positive integer");
   endif
 
   ## default n is different if image is logical
@@ -76,8 +76,14 @@
   ##       type conversion does that automatically.
   if (n <= 256)
     I = uint8 (I);
+  elseif (n <= 65536)
+    I = uint16 (I);
+  elseif (n <= 4294967296)
+    I = uint32 (I);
+  elseif (n <= intmax ("uint64"))
+    I = uint64 (I);
   else
-    I = uint16 (I);
+    error ("gray2ind: N is larger than uint64 max value");
   endif
 
 endfunction
@@ -101,6 +107,7 @@
 %! assert (class (gray2ind ([0.0 0.5 1.0], 255)), "uint8");
 %! assert (class (gray2ind ([0.0 0.5 1.0], 256)), "uint8");
 %! assert (class (gray2ind ([0.0 0.5 1.0], 257)), "uint16");
+%! assert (class (gray2ind ([0.0 0.5 1.0], 655537)), "uint32")
 
 ## Test input validation
 %!error gray2ind ()