Kyle
Kyle

Reputation: 115

Matrix multiplication with bitwise operators

I want to create a bitmask vector that masks which values are greater than a given value. Something like [1, 2, 3, 4, 5] * [>3, >3, >3, >3, >3] = [0, 0, 0, 1, 1]. I want to be able to run this on theano to get faster computation time for matrix operations. Is there a linear algebra procedure that can be written using bitwise operators or bits to create this bitmask? I am currently looping through this matrix and I would like to move the computation to a GPU using theano which requires more matrix multiplication. Thanks for any help.

Upvotes: 4

Views: 1763

Answers (1)

Israel Unterman
Israel Unterman

Reputation: 13510

You can get exactly what you want with logical operations between matrices. For example

print((np.r_[1, 2, 3, 4, 5] > 3))

will give

[False False False  True  True]

And if you want integers you can do

print((np.r_[1, 2, 3, 4, 5] > 3).astype(int) )

and get

[0 0 0 1 1]

Upvotes: 3

Related Questions