# [Numpy-discussion] Histograms via indirect index arrays

Travis Oliphant oliphant.travis at ieee.org
Fri Mar 17 01:00:02 CST 2006

```Mads Ipsen wrote:
> Hey,
>
> First of all, thanks for the new release.
>
> Here's another question regarding something I cannot quite understand:
>
> Suppose you want to update bins for a histogram, you might think you
> could do something like:
>
>   g = zeros(4,Int)
>   x = array([0.2, 0.2])
>   idx = floor(x/0.1).astype(int)
>   g[idx] += 1
>
> Here idx becomes
>
>    array([2, 2])
>
> In this case, I would naively expect g to end up like
>
>   array([0, 0, 2, 0])                     (1)
>
>
>   array([0, 0, 1, 0])                     (2)
>
> Is this intended? Just being plain novice-like naive, I would expect
> the slice operation g[idx] += 1 to do something like
>
Yes, this is intended (sort of --- this particular example isn't the
reason for the behavior though).

The issue is that the code g[idx] +=1 is equivalent in Python to

g[idx] = g[idx] + 1

Then g[idx] returns array([0,0]) as it should.  This new copy of the
array data then gets added to 1 resulting in array([1,1]).  This array
is then set into element 2 of g twice just as if you had done

g[2] = 1
g[2] = 1

Then end effect is you get a 1 out of the result.

Perhaps a little counter to the way you were thinking of the problem,
but very much how it must be due to the way Python translates the
statement g[idx] += 1 in this case.

There are, of course, other ways to do what you want to do.

-Travis

```