[Numpy-discussion] vectorizing histogram-like computation
Piotr Luszczek
luszczek at cs.utk.edu
Mon Jun 20 13:08:02 CDT 2005
Rick White wrote:
> On Mon, 20 Jun 2005, Piotr Luszczek wrote:
>
>
>>There is numarray.numeric.histogram and numarray.mlab.histogram.
>>What they do is this (more or less):
>>
>>for idx in arr:
>> histogram[idx] += 1
>>
>>What I want to do is:
>>
>>for idx in arr:
>> result[idx] ^= value
>>
>>My question is whether there is a way to do it (the XOR example)
>>without using a for loop.
>
>
> Is value the same for every idx? That's the way you've written it.
> If so, you could simply do this:
>
> result = (mlab.histogram(idx) & 1)*value
>
> I'm guessing that you want value to be an array of the same size as
> idx though, in which case this would not work.
Yes it makes a difference whether value is a single value or an array.
In my case `value' is an array but I think your solution would work as
well if there are no duplicates in `idx'. In my case there are duplicates
in `idx' so `& 1' part would yield a funny result.
Piotr
More information about the Numpy-discussion
mailing list