2

I have 2 boolean matrices in numpy and am using the .dot() function to multiply them and the results I get is a boolean matrix.

Is there a way to the get the sum of the product of the respective elements during the multiplication that I would get if I was doing matrix multiplication and the elements were either 1 or 0?

i.e. the elements in the resulting matrix should either be 0 or non-zero integers.

Thanks in advance.

1 Answer 1

2

Convert to int with astype.

Demo:

>>> import numpy as np
>>> np.random.seed(5)
>>> a = np.random.random([3,3]) > 0.5
>>> b = np.random.random([3,3]) > 0.5

Now a, b are arrays of dtype bool:

>>> a
array([[False,  True, False],
       [ True, False,  True],
       [ True,  True, False]], dtype=bool)

Multiply them as integers:

>>> np.dot(a.astype(np.int), b.astype(np.int))
array([[0, 0, 1],
       [0, 0, 1],
       [0, 0, 2]])
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.