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

Multiply Numpy n x 2 array by n x 1 array

Assuming I have a Numpy n x 2 array y: array([[1, 1], [2, 3], [1, 4], ...]) and a Numpy n x 1 array x: array([2, 4, 5, ...]), how can I efficiently obtain the following result, n x 2 array: array([2, 2], [8, 12], [5, 20], ...]), where each element (array) of the y is multiplied by corresponding value from array x?

I can do it via a cycle but looking for more performant approaches.

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 :

With an actual n-by-1 x, you could just do x * y. Your x is 1-dimensional, n-not-by-anything, so you’d have to reshape it first:

x.reshape((n, 1)) * y
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