[Numpy-discussion] Histograms via indirect index arrays

Mads Ipsen mpi at osc.kiku.dk
Tue Mar 14 13:24:11 EST 2006


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)

but instead you get

  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

  for i in range(len(I)):
    g[ idx[i] ] += 1

resulting in (1) and not (2).

// Mads




More information about the NumPy-Discussion mailing list