Commits

Mike Miller committed cd4f714

quantiz.m: Fix wrong results with single-element table (bug #39332)

* inst/quantiz.m: Fix wrong results with single-element quantization table.
Match the orientation of the output to the input. Add tests.

Comments (0)

Files changed (1)

     print_usage ();
   endif
 
-  qidx = length(table) - lookup(flipud(table(:)), x(:));
+  if (numel (table) == 1)
+    qidx = double (table < x);
+  else
+    qidx = length (table) - lookup (flipud (table(:)), x);
+  endif
+
   if (nargin > 2 && nargout > 1)
     q = codes(qidx + 1);
   endif
   endif
 
 endfunction
+
+%!assert (quantiz (1:10, 0:9), 1:10);
+%!assert (quantiz ([1:10]', 0:9), [1:10]');
+%!assert (quantiz (1:10, [3 6 9]), [0 0 0 1 1 1 2 2 2 3]);
+%!assert (quantiz (1:10, 5), [0 0 0 0 0 1 1 1 1 1]);
+%!assert (quantiz ([-Inf -1 0 1 Inf], [-1 0 1]), [0 0 1 2 3]);
+%!assert (quantiz ([-Inf -1 0 1 Inf], 0), [0 0 0 1 1]);
+
+%% Test input validation
+%!error quantiz ()
+%!error quantiz (1)
+%!error quantiz (1, 2, 3, 4)