Here is my problem:
How can I find the index of the histogram bin in which a number falls?
In Matlab, the solution is easy. HISTC does the job:
The hist
function will return the breakpoints between the bins if you do not already have them. You can then use the findInterval
function to find which interval/bin each of your points falls into:
> tmp <- hist(iris$Petal.Width)
> findInterval(iris$Petal.Width, tmp$breaks)
[1] 2 2 2 2 2 3 2 2 2 1 2 2 1 1 2 3 3 2 2 2 2 3 2 3 2
[26] 2 3 2 2 2 2 3 1 2 2 2 2 1 2 2 2 2 2 4 3 2 2 2 2 2
[51] 7 8 8 7 8 7 9 6 7 7 6 8 6 7 7 7 8 6 8 6 10 7 8 7 7
[76] 7 7 9 8 6 6 6 7 9 8 9 8 7 7 7 7 7 7 6 7 7 7 7 6 7
[101] 13 10 11 10 12 11 9 10 10 13 11 10 11 11 13 12 10 12 12 8 12 11 11 10 11
[126] 10 10 10 11 9 10 11 12 8 7 12 13 10 10 11 13 12 10 12 13 12 10 11 12 10
> tmp2 <- .Last.value
> cbind( value=iris$Petal.Width, lower=tmp$breaks[tmp2], upper=tmp$breaks[tmp2+1])
value lower upper
[1,] 0.2 0.2 0.4
[2,] 0.2 0.2 0.4
[3,] 0.2 0.2 0.4
[4,] 0.2 0.2 0.4
[5,] 0.2 0.2 0.4
[6,] 0.4 0.4 0.6
[7,] 0.3 0.2 0.4
[8,] 0.2 0.2 0.4
[9,] 0.2 0.2 0.4
[10,] 0.1 0.0 0.2
There are a couple of R packages that (re)implement histc
to ease porting code. For example,
pracma
http://cran.r-project.org/web/packages/pracma/