You can use NumPy broadcasting for pairwise elementwise multiplication between x and y and then flatten with .ravel(), like so -

(x[:,None]*y).ravel()

Or use outer product and then flatten -

np.outer(x,y).ravel()
Answer from Divakar on Stack Overflow
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ multiply-arguments-element-wise-with-different-shapes-in-numpy
Multiply arguments element-wise with different shapes in Numpy
February 7, 2022 - print(" Result (multiply element-wise)... ",np.multiply(arr1, arr2)) import numpy as np # Create two arrays with different shapes arr1 = np.arange(27.0).reshape((3, 3, 3)) arr2 = np.arange(9.0).reshape((3, 3)) # Display the arrays print("Array 1... ", arr1) print(" Array 2...
Discussions

matrix - Elementwise multiplication of NumPy arrays of different shapes - Stack Overflow
When I use numpy.multiply(a,b) to multiply numpy arrays with shapes (2, 1),(2,) I get a 2 by 2 matrix. But what I want is element-wise multiplication. I'm not familiar with numpy's rules. Can any... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How can arrays with different dimensions be multiplied? - Python - Data Science Dojo Discussions
I encountered an error while attempting to multiply two numpy arrays with different dimensions in my data science project. Could anyone kindly share insights on the correct ways to multiply matrices with disparate dimensions in the context of numpy arrays? Your assistance with a code snippet ... More on discuss.datasciencedojo.com
๐ŸŒ discuss.datasciencedojo.com
3
0
February 28, 2023
python 2.7 - numpy multiply arrays with different shapes - Stack Overflow
I have an array A of shape (w,h) = 3000,2000 and another array B of shape d = 100 I want to multiply each value of A by B, and get the result in the form of an array C of shape (w,h,d) = 3000,2000... More on stackoverflow.com
๐ŸŒ stackoverflow.com
July 18, 2016
python - how to multiply 2 numpy array with different dimensions - Stack Overflow
In Python with the numpy numerical library or the sympy symbolic library, multiplication of array objects as a1*a2 produces the Hadamard product, but with otherwise matrix objects m1*m2 will produce a matrix product. Simply speaking, slice it up to arrays and perform x*y, or use other routes to fit the requirement. ... So, if x has shape ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Medium
medium.com โ€บ @whyamit101 โ€บ different-ways-to-multiply-arrays-in-numpy-65aa2522e265
Different Ways to Multiply Arrays in NumPy | by why amit | Medium
February 9, 2025 - They donโ€™t align, and NumPy wonโ€™t perform the operation. Hereโ€™s the good news: reshaping arrays can often solve this issue. The .reshape() method lets you adjust an arrayโ€™s dimensions so they become compatible for multiplication. Letโ€™s take the same example but tweak it slightly: # Reshaping to make shapes compatible array1 = np.array([1, 2, 3]) array2 = np.array([[4], [5], [6]]) result = array1.reshape(3, 1) * array2 print("Reshaped arrays multiplication:\n", result)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ numpy-multiply-in-python
numpy.multiply() in Python - GeeksforGeeks
July 11, 2025 - numpy.multiply() supports broadcasting which means it can multiply arrays with different shapes as long as they are compatible for broadcasting rules.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.multiply.html
numpy.multiply โ€” NumPy v2.4 Manual
January 31, 2021 - numpy.multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'multiply'>#
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.multiply.html
numpy.multiply โ€” NumPy v2.1 Manual
>>> x1 = np.arange(9.0).reshape((3, 3)) >>> x2 = np.arange(3.0) >>> np.multiply(x1, x2) array([[ 0., 1., 4.], [ 0., 4., 10.], [ 0., 7., 16.]])
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @amit25173 โ€บ numpy-element-wise-multiplication-306fd4cb5841
NumPy Element-wise Multiplication | by Amit Yadav | Medium
January 25, 2025 - NumPy uses something called broadcasting, which adjusts array shapes to make operations possible. Imagine this: Youโ€™re trying to multiply a single column of numbers with an entire row of numbers.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ numpy โ€บ numpy_multiply_function.htm
NumPy multiply() Function
The NumPy multiply() function is used to perform element-wise multiplication between two arrays. It takes two input arrays (of the same shape or compatible shapes) and returns a new array with their corresponding elements multiplied.
๐ŸŒ
Data Science Dojo
discuss.datasciencedojo.com โ€บ python
How can arrays with different dimensions be multiplied? - Python - Data Science Dojo Discussions
February 28, 2023 - I encountered an error while attempting to multiply two numpy arrays with different dimensions in my data science project. Could anyone kindly share insights on the correct ways to multiply matrices with disparate dimensโ€ฆ
๐ŸŒ
Educative
educative.io โ€บ blog โ€บ numpy-matrix-multiplication
NumPy matrix multiplication: Get started in 5 minutes
2 weeks ago - Batch matrix multiplication means performing matrix multiplication across a stack of matrices at once. ... NumPy treats higher-dimensional arrays (3D or more) as a stack of matrices. ... Output shape: (2, 2, 2) Each matrix in the batch is multiplied independently.
๐ŸŒ
Medium
medium.com โ€บ @heyamit10 โ€บ numpy-multiply-in-python-7914322b2888
numpy.multiply() in Python. If you think you need to spend $2,000โ€ฆ | by Hey Amit | Medium
February 8, 2025 - You might have noticed that numpy.multiply() works like magic for handling different shapes, scalars, and practical tasks like scaling.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ numpy โ€บ numpy-multiply
NumPy multiply() - Element-wise Multiplication of Two Arrays
February 2, 2025 - import numpy as np # Define two arrays of the same shape x1 = np.array([1, 2, 3, 4]) x2 = np.array([5, 6, 7, 8]) # Perform element-wise multiplication result = np.multiply(x1, x2) # Print the result print("Element-wise multiplication result:", ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.broadcasting.html
Broadcasting โ€” NumPy v2.4 Manual
In the simplest example of broadcasting, the scalar b is stretched to become an array of same shape as a so the shapes are compatible for element-by-element multiplication.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ element-wise multiply arrays with different axes [numpy]
r/learnpython on Reddit: Element-wise multiply arrays with different axes [numpy]
February 23, 2016 -

Hi everyone,

I'm trying to achieve something with the least possible overhead. I have a one-dimensional array A whose shape is (N,) and another one B whose shape is (M,N). What I'm trying to do is to element-wise multiply each column of B (axis 1) by A. Basically something like this:

C = np.zeros((M,N))
for m in range(M):
    C[m,:] = A*B[m,:]

This can also be achieved more succintly with:

C = np.tile(A, (M, 1))*B

The problem with the first approach is that it requires a for, and the problem with the second is that is unnecessarily creates a temporary array. I was wondering if there was some way to do this with some funky numpy stuff, perhaps with some tricky broadcasting but I haven't been able to come up with anything. Hopefully you will!!