Matrix multiplication is a fundamental operation that combines two matrices. Unlike simpler element-by-element operations, matrix multiplication relies on a specific rule involving the rows of the first matrix and the columns of the second, rather than just corresponding entries. This operation is central to many concepts in machine learning, such as transforming data points, chaining computational steps in neural networks, and expressing systems of linear equations.
Before you can multiply two matrices, say and , they must satisfy a specific condition regarding their dimensions. If matrix has dimensions (meaning rows and columns) and matrix has dimensions ( rows and columns), then the matrix product is defined.
The critical part is that the number of columns in the first matrix () must equal the number of rows in the second matrix (). In this case, both are .
The resulting matrix, let's call it , will have dimensions . It will have the same number of rows as the first matrix () and the same number of columns as the second matrix ().
If the inner dimensions don't match (), the matrices cannot be multiplied in that order.
How do we find the values inside the resulting matrix ? Each element (the element in the -th row and -th column of ) is calculated by taking the dot product of the -th row of matrix and the -th column of matrix .
Remember the dot product of two vectors and is .
For matrix multiplication , where is and is :
Mathematically, if is the element in the -th row and -th column of , and is the element in the -th row and -th column of , then:
This means you multiply corresponding elements from the row of and the column of and then sum up those products.
A visual representation of how Row of matrix and Column of matrix combine via the dot product to compute the element in the resulting matrix .
Let's multiply a matrix by a matrix . The result should be a matrix.
The resulting matrix is :
Let's calculate each element:
So, the resulting matrix is:
NumPy makes matrix multiplication straightforward. The standard way to perform matrix multiplication between two NumPy arrays (representing matrices) since Python 3.5 is using the @ operator.
Let's perform the same calculation as above using NumPy:
import numpy as np
# Define matrices A and B
A = np.array([[1, 2, 3],
[4, 5, 6]])
B = np.array([[7, 8],
[9, 1],
[2, 3]])
# Check shapes
print(f"Shape of A: {A.shape}") # Output: Shape of A: (2, 3)
print(f"Shape of B: {B.shape}") # Output: Shape of B: (3, 2)
# Perform matrix multiplication using the @ operator
C = A @ B
print(f"\nMatrix A:\n{A}")
print(f"Matrix B:\n{B}")
print(f"Result C = A @ B:\n{C}")
# Output:
# Result C = A @ B:
# [[31 19]
# [85 55]]
print(f"Shape of C: {C.shape}") # Output: Shape of C: (2, 2)
The result matches our manual calculation. Notice how NumPy handles the row-by-column dot products internally.
You might also encounter np.dot(A, B) or A.dot(B). For 2D arrays (matrices), these functions perform standard matrix multiplication, just like the @ operator. However, the @ operator is generally preferred for matrix multiplication because it's unambiguous, whereas np.dot behaves differently for arrays with more than two dimensions. For clarity when working with matrices, stick with @.
Unlike multiplication with regular numbers (scalars), where , matrix multiplication is generally not commutative. This means that, in most cases:
Sometimes, might not even be defined even if is. For instance, in our example above, is and is . The product is defined and results in a matrix.
What about ? Here, is and is . The inner dimensions match (2 and 2), so is defined. The resulting matrix will be .
Since is and is , they clearly cannot be equal. Let's compute with NumPy to see:
# Calculate BA (note the order)
C_BA = B @ A
print(f"\nResult BA = B @ A:\n{C_BA}")
# Output:
# Result BA = B @ A:
# [[ 39 54 69]
# [ 13 23 33]
# [ 14 19 24]]
print(f"Shape of BA: {C_BA.shape}") # Output: Shape of BA: (3, 3)
As expected, is a matrix and is completely different from .
Even if and are square matrices of the same size, where both and are defined and have the same dimensions, the results will usually be different. The order in which you multiply matrices matters significantly. This has important implications in areas like computer graphics and machine learning, where sequences of matrix operations represent sequences of transformations or computational steps. Changing the order changes the outcome.
Was this section helpful?
matmul function and the @ operator, detailing their behavior for matrix multiplication with examples.© 2026 ApX Machine LearningEngineered with