Faster way to find the quantization level of a sample
As hinted by Florian, to quantise any $x[n] \in \mathbb{R}$ to a $y[n] \in \mathbb{Z}$ with $K$ levels you can apply the function:

$$y[n] = round\left( \frac{x[n]-min(x)}{range(x)} (K-1)  \right)$$

Where $n \in [0,1,2..|x|]$ and $|.|$ denotes the length of the sequence. 

This will map your $x$ from its $min(x)..max(x)$ range to a $[0..K)$ discrete steps range.

For example (in Octave):

Fs = 100; #Sampling frequency (in Hz)
f = 4; # Frequency of a simple sinusoidal signal (in Hz)
T = 1; # Timeframe length (in seconds)
K = 5; # Number of levels to quantise to
t = 0:(1./Fs):(T-(1./Fs));
p = 2.0 * pi * t;
x = sin(f*p);
y = round(((x-min(x))/2.0) * (K-1));
plot(y);
xlabel("Discrete time (sample)");
ylabel("Amplitude");
grid on;


Which produces:



You might also like to see uencode

Hope this helps.