Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vectorising a sum of scalar multiplied by a matrix, where the scaler is an element of a list

I’m trying to vectorise the following

a = np.array([1,2])
b = np.array([[5,5],[5,5]])
target = 0
for _ in a:
    target = target + _ * b
    

The above yields a 2×2 matrix where all entries are 15. How can I achieve this through vectorisation? I’ve been trying to cast a to be two 2×2 matrices, but I’m not getting anywhere

Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This looks like a simple:

b * a.sum()

Output:

array([[15, 15],
       [15, 15]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading