On 29-Apr-09, at 5:06 PM, Dan Goodman wrote: > Here's the problem I want to write vectorised code for. I start with > an > array of indices, say I=array([0,1,0,2,0,1,4]), and I want to come up > with an array C that counts how many times each index has been seen so > far if you were counting through the array from the beginning to the > end, e.g. for that I, C=array([0,0,1,0,2,1,0]). This is easy enough to > do with a C loop, but trickier to do just with numpy. Any ideas?
How about this: In [28]: I = array([0,1,0,2,0,1,4]) In [29]: C = empty(len(I),dtype=int) In [30]: for idx in unique(I): ....: V = I == idx ....: C[V] = arange(len(V)) ....: In [31]: C Out[31]: array([0, 0, 1, 0, 2, 1, 0]) David _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion