|
From: Robert K. <rk...@uc...> - 2005-07-14 06:23:48
|
Graeme O'Keefe wrote: > Hi, > > I know this really belongs on the sourceforge.net:numarray list, but it > is a very inactive list. It's still the right place to ask. Your question would get answered in a timely manner. We're quiet; we're not gone. :-) Followup-to has been reset. > I have noted the following with numarray: > > >>> x = numarray.zeros([3,3], numarray.Int16) > >>> i = [0,1,2,0,1,2] > >>> j = [0,1,2,0,1,2] > >>> x[i, j] += 1 > >>> print x > [[1, 0, 0] > [0, 1, 0] > [0, 0, 1]] > I was expecting (hoping) > [[2, 0, 0] > [0, 2, 0] > [0, 0, 2]] > > which is what you obviously get if you: > > for n in range(len(i)) : x[i[n], j[n]] += 1 > > This is the sort of operation one does all the time when histogramming > data-streams. Is there a way to achieve this without looping over i/j. This has been discussed recently on the numpy-discussion list (Subject: vectorizing histogram-like computation). The semantics of array indexing aren't anything like that for loop. I doubt that the two kinds of semantics could be merged consistently. x[i,j] += 1 is closer to x[i,j] = x[i,j] + 1 which amounts, in this case, to x[i,j] = array([1, 1, 1, 1, 1, 1]) There's nothing in this last operation to suggest that the numbers ought to be added together (and if the initial values weren't zero, a more complicated operation would have to considered). In short, I consider it cleaner to keep specialized histogram code tucked away in a function which you can eventually optimize to C as required. -- Robert Kern rk...@uc... "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter |