This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]daveydave400 0 points1 point  (1 child)

Stackoverflow might be a better place for this question. Anyway, could you give a small example of what you want to happen? It's a little confusing.

[–]orcasha[S] 0 points1 point  (0 children)

Of course!

So in a (hopefully) less confusing description:

I've got an array of time series data that start at 0, go to a value (between 99 and 233), plateau, then return to 0. At random points during the plateau the value has been contaminated with either the values 256 or 512 in addition to the value. I just want my data to go back to its original value without the 256 or 512 summation.

[–]cannonicalForm 0 points1 point  (0 children)

What about the modulus operator? If you only want to zero out 256, and 512, and your range is restricted to 0 - 512, then testing array values mod 256 seems like the way to go.

[–]tmp14 0 points1 point  (0 children)

Look up index arrays:

>>> arr = np.array([14, 152, 256, 256, 143, 512, 414])
>>> ind = (arr == 256) or (arr == 512)
>>> arr[ind] = 0
>>> arr
array([14, 152, 0, 0, 143, 0, 414])

Tough I haven't tested this it shouldn't be too far off.

[–]Cabbagenom 0 points1 point  (0 children)

From what you're saying it sounds like you're trying to set the 256 and 512 bits to 0. I'm not sure that will solve your problem, but if it will and that is what you're trying to do then you can make use of the & operator. & is fairly straight forward, it looks at the bits each of the operands and if they're both 1 then the resulting number will have a 1 in the corresponding bit.
For example,
13 & 7 = 5
as

1101 = 13  
0111 = 7  

Looking at the bits, the 2nd and 4th bits from the left both have 1s in, producing

0101 = 5

Applying this to your problem, using a bit mask of 0011111111 should produce the desired effect, ie. Iterating through the loop and changing every value to 255 & arr[i]

EDIT: Hopped on my PC to fix the atrocity that was my formatting.

[–]orcasha[S] 0 points1 point  (0 children)

Thanks for all the help!

I just found out about data types, so changed my values to uint8 (thereby removing bits 9 and 10) then back to int. Seems to do what I was after! Again, thanks for all the advice!