Usage
quantize(x, bins=c(-1,0,1), metric=function(a,b) abs(a-b))Details
When converting analog signals to digital signals,
quantization is a natural phenomenon. This concept can be extended
to contexts outside of DSP. More generally it can be thought of
as a way to classify a sequence of numbers according to some
arbitrary distance function. The default distance function is the Euclidean distance in 1 dimension.
For the default set of bins, values from (-infty, -.5] will map to -1.
The values from (-.5, .5] map to 0, and the segment (.5, infty) map to 1.
Regardless of the ordering of the bins, this behavior is
guaranteed. Hence for a collection of boundary points k and bins b,
where |b| = |k| + 1, the mapping will always have the form
(-infty, k_1] => b_1, (k_1, k_2] => b_2, ... (k_n, infty) => b_n.