Run NumPy Online

Matrix Multiplication with NumPy (@ and matmul)

Multiply matrices with the @ operator and see exactly how each result cell is built from a row and a column.

  • Intermediate
  • 2 min read
  • 1 runnable example
What you'll learn
  • The @ operator and np.matmul
  • Row-times-column rule
  • Shape rules: (m, n) @ (n, p)
[[1, 2], [3, 4]] @ [[5, 6], [7, 8]]
row 0 · col 01×5 + 2×7 = 19
row 0 · col 11×6 + 2×8 = 22
row 1 · col 03×5 + 4×7 = 43
row 1 · col 13×6 + 4×8 = 50

Row times column

The problem

Matrix multiplication is behind graphics transformations, neural networks and systems of equations. It is not the same as multiplying matching numbers, and using the wrong operator gives a wrong result without any error.

Think of it like this

Matrix multiplication is like a shop totaling orders. Each customer's row lists how many of each item they bought; each price column lists what each item costs. A customer's bill is the sum of quantity × price across one row and one column.

From shop bills to code
left🧾 Rows: what each customer bought
right💲 Columns: the prices
left @ right✖️ Row times column, then add up
19 = 1×5 + 2×7🧮 One cell of the result
Each result cell combines a row of the left matrix with a column of the right one.
In one sentence

Matrix multiplication (@) produces a matrix whose cell [i, j] is the sum of products of row i of the left matrix and column j of the right matrix.

Key points

  1. left @ right performs matrix multiplication.
  2. The top-left result 19 is 1×5 + 2×7: row 1 of left times column 1 of right.
  3. The number of columns of left must equal the number of rows of right.
  4. left * right would instead multiply matching cells, giving a different result.

When should you use it?

  • Linear algebra: transformations, rotations, systems of equations.
  • Neural network layers, which are matrix multiplications.
  • Combining tables of quantities and rates, such as orders and prices.

When not to use it

  • Scaling or combining cell by cell: use *.
  • Shapes that do not fit: (2, 3) @ (2, 3) fails; transpose one side if that is what you mean.

Example code

Run code →
main.py
import numpy as np

left = np.array([[1, 2], [3, 4]])
right = np.array([[5, 6], [7, 8]])

product = left @ right
print(product)
Get a Run button for this example

Result of running the code

[[19 22]
 [43 50]]

Code explanation

The @ operator performs matrix multiplication. It differs from the * operator, which multiplies matching elements.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input shape and dtype.
  2. Try negative, floating-point, and missing values.
  3. Apply the operation to larger multidimensional arrays and measure performance.
Run in the Python compiler →

Common mistakes

Using * for matrix multiplication

* multiplies cell by cell and silently gives the wrong answer.

Problem
product = left * right
Fix
product = left @ right

Finished this lesson?

Mark it as learned to track your progress.

Next: pandas DataFrame →

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges