Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy xor-reduce an array

How to xor all elements of a boolean numpy array using vectorized methods: i.e., a_1 xor a_2 xor ... xor a_n?

like image 929
Yariv Avatar asked Feb 13 '13 09:02

Yariv


1 Answers

I would prefer using the xor ufunc I think, which is bitwise_xor (or logical_xor):

np.bitwise_xor.reduce(a)

or:

np.logical_xor.reduce(a)

One advantage is, you don't get bogus stuff for floats.

like image 164
seberg Avatar answered Oct 19 '22 05:10

seberg