10

For a given NumPy array, it is easy to perform a "normal" sum along one dimension. For example:

X = np.array([[1, 0, 0], [0, 2, 2], [0, 0, 3]])
X.sum(0)
    =array([1, 2, 5])
X.sum(1)
    =array([1, 4, 3])

Instead, is there an "efficient" way of computing the bitwise OR along one dimension of an array similarly? Something like the following, except without requiring for-loops or nested function calls.

Example: bitwise OR along zeroeth dimension as I currently am doing it:

np.bitwise_or(np.bitwise_or(X[:,0],X[:,1]),X[:,2])
    =array([1, 2, 3])

What I would like:

X.bitwise_sum(0)
    =array([1, 2, 3])

1 Answer 1

16
numpy.bitwise_or.reduce(X, axis=whichever_one_you_wanted)

Use the reduce method of the numpy.bitwise_or ufunc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.