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

python - Efficient multiplying matrices with different shapes in numpy - Stack Overflow
I think that by 'multiplication' he actually meant doing something to the matrices, but he didn't formulate it right so it's unknown what happens in certain situations. 2018-08-10T12:10:35.747Z+00:00 ... import numpy as np np_1= np.arange(15).reshape(5,3) np_2=np.arange(6).reshape(3,2) ... More on stackoverflow.com
🌐 stackoverflow.com
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
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
How does the matrix multiplication of different shapes is working?
https://en.m.wikipedia.org/wiki/Matrix_multiplication More on reddit.com
🌐 r/learnmachinelearning
6
1
October 10, 2021
🌐
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
Input arrays to be multiplied. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).
🌐
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.
Find elsewhere
🌐
Educative
educative.io › blog › numpy-matrix-multiplication
NumPy matrix multiplication: Get started in 5 minutes
April 30, 2026 - 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.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.multiply.html
numpy.multiply — NumPy v2.2 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.]])
🌐
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.
🌐
CompleteEra
completeera.com › numpy-multiply-array-elements-techniques
Numpy Multiply Array Elements: Techniques - CompleteEra
May 5, 2026 - For **non-standard operations**, wrap functions: “`python def custom_mult(a, b): return a * b + 1 vectorized = np.vectorize(custom_mult) result = vectorized(arr1, arr2) “` **Note**: Slower than pure NumPy, but useful for **custom logic**. ... **Error**: `ValueError: shapes (2,) and (3,) not aligned` **Fix**: Ensure arrays have **compatible shapes** or use **broadcasting**. “`python arr1 = np.array([1, 2]) # Shape (2,) arr2 = np.array([3, 4, 5]) # Shape (3,) # Solution: Reshape or broadcast arr2_broadcast = np.broadcast_to(arr2, (2, 3)) # Now shape (2, 3) result = arr1 * arr2_broadcast “` – **`*`**: Element-wise multiplication.
🌐
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.
🌐
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.
🌐
Reddit
reddit.com › r/learnmachinelearning › how does the matrix multiplication of different shapes is working?
r/learnmachinelearning on Reddit: How does the matrix multiplication of different shapes is working?
October 10, 2021 -

In middle school, I learnt that matrices can only be added if they have the same shape (m x n) and (p x q) addition or subtraction is only possible if m == p and n == q. The operation is element-wise and that makes sense

How come different shape in NumPy array is added with each other

>>> a = np.random.randint(0, 5, (2, 3))
>>> b = np.random.randint(0, 5, (3, 1))
>>> c = np.random.randint(0, 5, (1, ))
>>> a
array([[4, 4, 4],
       [4, 2, 4]])
>>> b
array([[0],
       [3],
       [0]])
>>> c
array([2])
>>> matmul = np.matmul(a, b)
>>> matmul
array([[12],
       [ 6]])
>>> matmul.shape
(2, 1)
>>> c.shape
(1,)
>>> matmul + c
array([[14],
       [ 8]])
>>> 

In the above code, I couldn't understand the last 3 steps

UPDATE: I want to know about matrix multiplication addition, my bad.

🌐
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…
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_array_multiplication.htm
NumPy - Array Multiplication
For element-wise multiplication to be performed, the two arrays must have the same shape. If the arrays are of different shapes, broadcasting rules are applied to make them compatible.