AI, machine learning and deep learning · Introductory

Multiplying Matrices

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

Matrix multiplication involves two distinct operations: the dot product, which transforms data spaces by combining rows and columns, and element-wise multiplication, which operates on corresponding entries. In Python, the @ operator or np.dot executes the dot product, while the * operator performs element-wise multiplication. Using the wrong operator leads to incorrect results because they produce fundamentally different mathematical outcomes.

Intuition

Think of matrix multiplication not as a single concept, but as two separate tools for different jobs. The dot product is like a complex mixer that takes the structure of one matrix and reshapes it using the structure of another, essential for transforming data in machine learning. Element-wise multiplication is simpler, like pairing up socks; you only multiply items that are in the exact same position. The key is recognizing that these operations have different rules for compatibility and different results, so choosing the right 'tool' (operator) in your code is critical to getting the intended mathematical behavior.

Key equations

C=A@B
(1)

The resulting matrix C from the dot product of matrices A and B, where each element is the sum of the products of corresponding row elements from A and column elements from B.

Cij=Aij∗Bij
(2)

The resulting matrix C from element-wise multiplication, where each element C_{ij} is the product of the corresponding elements A_{ij} and B_{ij} from the input matrices.

Derivation

  1. Identify the two types of matrix multiplication: dot product and element-wise multiplication.
  2. For the dot product, verify that the number of columns in the first matrix equals the number of rows in the second matrix.
  3. Compute the dot product by taking the dot product of rows from the first matrix and columns from the second matrix.
  4. For element-wise multiplication, verify that both matrices have the exact same size.
  5. Multiply corresponding entries of the two matrices together to produce the result.

Worked example

Problem

Given two 2x2 matrices A and B, calculate the result using the dot product operator @ in Python.

  1. Define matrix A and matrix B as NumPy arrays.
  2. Apply the @ operator to A and B (A @ B).
  3. The result is a new 2x2 matrix where each entry is the sum of products of the respective row from A and column from B.

Answer A 2x2 matrix resulting from the dot product, distinct from the element-wise result.

Common mistakes

  • Using the * operator in NumPy performs element-wise multiplication, not the dot product, which is a common mistake for those expecting standard matrix multiplication behavior.
  • Linear indexing in MATLAB can lead to incorrect results without error messages, especially if the 'N-1 commas' rule for indexing is not followed.

Animation

Animation coming soon

The animated version of this lesson is being prepared for the web. In the Signals AI app, lessons are drawn step by step.

Code

This is a text lesson: it has no animation code yet.

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.