MATLAB: How do i discretize negative integers

discretize

[~, discrete_x] = histc(x, edges);
discrete_x(discrete_x == length(edges)) = length(edges)-1;
discrete_x(discrete_x == 0) = NaN;
This works for positive integers only. what do i do if i have to do it for negative integers?

Best Answer

"This works for positive integers only"
Actually histc works perfectly for negative values. It works for me:
>> x = 4-randi(9,1,10)
x =
-2 -5 -5 1 -1 -5 1 1 2 0
>> edges = -6:4:6
edges =
-6 -2 2 6
>> [~, idx] = histc(x, edges)
idx =
2 1 1 2 2 1 2 2 3 2
>> vec = x(idx)
vec =
-5 -2 -2 -5 -5 -2 -5 -5 -5 -5