What Is Matrix Multiplication?
Matrix multiplication is a fundamental operation in linear algebra that combines two matrices to produce a third matrix. Unlike scalar multiplication where you simply multiply two numbers, matrix multiplication follows a specific pattern: each entry in the result is the dot product of a row from the first matrix and a column from the second matrix.
Formally, if A is an m x n matrix and B is an n x p matrix, their product C = AB is an m x p matrix where each element is defined as:
In plain language: to find the value in row i, column j of the result, you take the entire row i from the first matrix and the entire column j from the second matrix, multiply them element by element, and add the products together. This operation is known as the dot product (or inner product).
Here is a visual diagram showing how a single cell in the result matrix is computed. The highlighted row from Matrix A and the highlighted column from Matrix B combine to form one element in the result:
This process repeats for every cell in the result matrix. A 2x2 times 2x2 multiplication requires 4 dot products. A 3x3 times 3x3 requires 9 dot products. Each dot product involves n multiplications and n-1 additions, where n is the shared dimension.
Interactive Matrix Multiplication Calculator
Use this calculator to multiply any two matrices up to 6x6. Enter your values, then click Calculate for the instant result or Step-by-Step to walk through each cell computation with highlighting.
Matrix Multiplication Calculator
The Dimension Rule: When Can You Multiply?
Not all pairs of matrices can be multiplied. The dimension compatibility rule is the single most important prerequisite to check before attempting matrix multiplication.
The inner dimensions must match. The number of columns in Matrix A must equal the number of rows in Matrix B. If they do not match, the multiplication is undefined, meaning it cannot be performed.
Examples of Compatible Dimensions
- (2x3) × (3x2) = 2x2 result. The inner dimension 3 matches.
- (3x3) × (3x3) = 3x3 result. Square matrices of equal size are always compatible.
- (4x2) × (2x5) = 4x5 result. The inner dimension 2 matches.
- (1x4) × (4x1) = 1x1 result. A row vector times a column vector gives a scalar.
Examples of Incompatible Dimensions
- (2x3) × (2x3) cannot be multiplied. Columns of A (3) does not equal rows of B (2).
- (4x4) × (3x4) cannot be multiplied. Columns of A (4) does not equal rows of B (3).
This rule also shows why matrix multiplication is generally not commutative. Even if AB is defined, BA might have different dimensions or might not be defined at all. For example, a (2x3) times (3x4) gives a 2x4 matrix, but (3x4) times (2x3) cannot be computed because the inner dimensions (4 and 2) do not match.
Step-by-Step Walkthrough
2x2 Matrix Multiplication
Let us multiply two 2x2 matrices step by step. This is the simplest non-trivial case and the best place to build intuition.
A 2x2 multiplication requires 4 dot products, each involving 2 multiplications and 1 addition, for a total of 8 multiplications and 4 additions.
3x3 Matrix Multiplication
Now let us tackle a 3x3 example. The process is identical but with more cells to compute. Each dot product now involves 3 terms.
The remaining cells follow the same pattern. A 3x3 multiplication requires 9 dot products with 3 multiplications each, giving 27 multiplications and 18 additions total. You can use the interactive calculator above to verify these results and see all 9 cells computed step by step.
Properties of Matrix Multiplication
Matrix multiplication has several important algebraic properties that distinguish it from regular number multiplication. Understanding these properties is essential for working with matrices in any applied context.
This is the most important distinction from scalar multiplication. Changing the order of matrices usually gives a different result. In fact, even if AB is defined, BA might not be defined at all. For square matrices where both products exist, the results are typically different. There are special cases where AB = BA (for example, when B is the identity matrix, or when both matrices are diagonal), but these are exceptions rather than the rule.
You can group matrix multiplications in any order without changing the result. While the final answer is the same, the choice of grouping can dramatically affect computational cost. The matrix chain multiplication problem exploits this property to find the most efficient grouping, potentially reducing billions of operations to millions.
Matrix multiplication distributes over matrix addition, both from the left and from the right. Note that the order matters because multiplication is not commutative: A(B + C) and (B + C)A are generally different.
The transpose of a product is the product of transposes in reverse order. This "reversal" property extends to products of three or more matrices: (ABC)^T = C^T B^T A^T. This property is used extensively in deriving machine learning backpropagation equations and in optimization theory.
The identity matrix I acts like the number 1. Multiplying any matrix by the identity matrix (of compatible size) returns the original matrix unchanged. This property is fundamental to matrix inverses, where A times A-inverse equals the identity.
Unlike regular numbers, two nonzero matrices can multiply to give the zero matrix. For example, multiplying [[1, 0], [0, 0]] by [[0, 0], [0, 1]] gives the 2x2 zero matrix, even though neither input is zero. This makes "dividing" by a matrix (when possible) a more delicate operation than dividing real numbers.
Special Matrices in Multiplication
Certain matrices have special behavior under multiplication that makes them particularly useful in both theory and applications.
Identity Matrix (I)
The identity matrix is a square matrix with 1s on the diagonal and 0s everywhere else. It is the multiplicative neutral element: AI = IA = A for any compatible matrix A. The 3x3 identity matrix looks like:
Zero Matrix (0)
A matrix where every entry is 0. Multiplying any matrix by a zero matrix of compatible size gives a zero matrix: A × 0 = 0. The zero matrix acts like the number 0 in multiplication.
Diagonal Matrix
A square matrix where all off-diagonal entries are zero. Multiplying by a diagonal matrix scales each row (left multiplication) or each column (right multiplication) independently. Diagonal matrices commute with each other, providing one of the rare cases where AB = BA. They are extremely efficient to work with computationally since the multiplication reduces from O(n^3) to O(n^2).
Symmetric Matrix
A matrix equal to its own transpose: A = A^T. While the product of two symmetric matrices is not generally symmetric, the expression A^T A is always symmetric for any matrix A. This appears everywhere in statistics (covariance matrices) and optimization (normal equations for least squares).
Orthogonal Matrix
A square matrix whose transpose equals its inverse: Q^T Q = QQ^T = I. Orthogonal matrices preserve lengths and angles, making them essential for rotations in computer graphics and physics simulations. Their inverses are trivially computed by transposition.
Real-World Applications
Matrix multiplication is not an abstract exercise. It powers critical computations across nearly every field of science and engineering. Here are the most significant applications.
Computer Graphics
Every 3D object you see on screen is transformed by matrix multiplication. Translation, rotation, scaling, and perspective projection are all encoded as 4x4 matrices. Your GPU performs billions of matrix multiplications per second to render each frame.
Machine Learning
Neural networks are fundamentally chains of matrix multiplications followed by nonlinear activations. A single layer computes y = Wx + b, where W is a weight matrix. Training adjusts these matrices through backpropagation, which itself relies on matrix calculus.
Cryptography
The Hill cipher encrypts text by multiplying plaintext vectors by a key matrix over a finite field. Modern lattice-based cryptography, considered quantum-resistant, relies on the hardness of certain problems expressed as matrix equations.
Physics Simulations
Quantum mechanics describes states as vectors and observables as matrices. The time evolution of quantum systems is computed through matrix exponentiation. Classical mechanics uses rotation matrices to transform coordinate systems.
Economics & Finance
Input-output models by Leontief use matrix multiplication to model how industries depend on each other. Portfolio optimization, risk analysis (covariance matrices), and Markov chain models for market states all rely heavily on matrix operations.
Search Engines
Google's original PageRank algorithm computes the dominant eigenvector of a web link matrix through repeated matrix-vector multiplication (power iteration). This determines the relative importance of every page on the internet.
Common Mistakes and How to Avoid Them
Matrix multiplication trips up beginners (and sometimes experts) in predictable ways. Here are the most frequent errors and how to avoid each one.
Mistake: Multiplying element by element.
Many beginners compute C(i,j) = A(i,j) × B(i,j). This is the Hadamard product, a valid operation but completely different from standard matrix multiplication. True matrix multiplication uses the dot product of rows and columns, not element-wise pairing.
Fix: Always take row i from A and column j from B, multiply corresponding pairs, and sum.
Mistake: Assuming AB = BA.
Treating matrices like regular numbers and swapping order is one of the most dangerous errors in linear algebra. It can silently produce wrong results in derivations, code, and proofs.
Fix: Always preserve the order. Write out which matrix is on the left and which is on the right. If you need BA, compute it separately.
Mistake: Ignoring dimension compatibility.
Attempting to multiply a (3x2) matrix by a (3x4) matrix without checking that the inner dimensions match. Many programming errors stem from shape mismatches that produce confusing runtime errors.
Fix: Before multiplying, always verify that the number of columns in A equals the number of rows in B. Write down the dimensions: (m x n)(n x p) = (m x p).
Mistake: Losing track of which row and column you are computing.
In larger matrices, it is easy to use the wrong row or column, especially when doing computations by hand. One misaligned index propagates errors through the entire result.
Fix: Label each computation explicitly. Write "C(2,3) = Row 2 of A dot Col 3 of B" before computing. Use the step-by-step calculator above to check your work.
Mistake: Forgetting to sum the products.
Computing the individual products (a1*b1, a2*b2, ...) but forgetting to add them together, leaving a vector instead of a scalar for each cell.
Fix: The dot product always produces a single number. If your result for one cell has multiple values, you have not finished the computation.
Computational Complexity
How expensive is matrix multiplication? This question has driven decades of research in computer science and remains an active area of investigation. The answer depends on both the algorithm used and the matrix size.
For two n x n matrices, the naive algorithm (the row-times-column method we have been using) requires exactly n^3 multiplications and n^2(n-1) additions, giving an overall time complexity of O(n^3). Doubling the matrix size increases computation time by a factor of 8.
In 1969, Volker Strassen discovered a breakthrough: you can multiply two 2x2 matrices using only 7 multiplications instead of the naive 8, at the cost of more additions. Applied recursively, this reduces the asymptotic complexity to O(n^2.807). For large matrices (typically n > 64), Strassen's algorithm provides a measurable speedup.
| Algorithm | Complexity | Year | Practical Use |
|---|---|---|---|
| Naive (schoolbook) | O(n^3) | Classical | Standard for small matrices |
| Strassen | O(n^2.807) | 1969 | Used in practice for large n |
| Coppersmith-Winograd | O(n^2.376) | 1990 | Theoretical only |
| Alman-Williams | O(n^2.3728) | 2024 | Theoretical only |
| Theoretical lower bound | O(n^2) (conjectured) | Open | Unknown if achievable |
In practice, most matrix multiplication in scientific computing uses highly optimized implementations of the naive algorithm that exploit CPU cache hierarchies, SIMD instructions, and GPU parallelism. Libraries like BLAS, cuBLAS, and Intel MKL achieve near-theoretical-peak performance by carefully controlling memory access patterns. The constant factors in these optimized implementations often matter more than the asymptotic improvement of Strassen's algorithm for matrices under a few thousand rows.
For extremely large matrices common in deep learning (such as weight matrices in large language models with dimensions of 10,000+), GPU-accelerated matrix multiplication using tensor cores achieves throughputs exceeding 300 teraflops on modern hardware, making matrix multiplication one of the most optimized operations in all of computing.
Frequently Asked Questions
Matrix multiplication is an operation that takes two matrices and produces a new matrix. Each element of the result is the dot product of a row from the first matrix and a column from the second matrix. It requires the number of columns in the first matrix to equal the number of rows in the second matrix. The result of multiplying an m x n matrix by an n x p matrix is an m x p matrix.
To multiply two 3x3 matrices, compute each of the 9 cells in the result by taking the dot product of the corresponding row from Matrix A and column from Matrix B. Each dot product involves multiplying 3 pairs of numbers and summing the results. For cell (i,j), multiply each element in row i of A by the corresponding element in column j of B, then add them up. The output is also a 3x3 matrix with 27 individual multiplications.
Matrix multiplication is not commutative because AB does not generally equal BA. The row-column dot product structure means swapping the order changes which rows and columns are paired, producing different results. In many cases, if AB is defined, BA may not even be defined due to incompatible dimensions. Even for square matrices of the same size, AB and BA produce different values except in special cases (like when one matrix is the identity or both are diagonal).
For two matrices to be multiplied, the number of columns in the first matrix must equal the number of rows in the second matrix. If Matrix A is m x n and Matrix B is n x p, the product AB will be an m x p matrix. The shared dimension n is consumed in the dot product computation. If this dimension requirement is not met, the multiplication is undefined and cannot be performed.
The naive algorithm for multiplying two n x n matrices has O(n^3) time complexity, requiring n^3 multiplications. Strassen's algorithm (1969) reduces this to approximately O(n^2.807) using a clever divide-and-conquer approach. The current best theoretical bound is roughly O(n^2.3728) from Alman-Williams (2024), though this is impractical. It is conjectured that O(n^2) might be achievable, but this remains an open problem in computer science.
No. A 2x3 matrix has 3 columns, but a 2x3 matrix has 2 rows. Since the number of columns in the first matrix (3) does not equal the number of rows in the second matrix (2), the multiplication is undefined. However, you could multiply a 2x3 matrix by a 3x2 matrix (result: 2x2) or multiply the 2x3 matrix by a 3x3 matrix (result: 2x3).
Standard matrix multiplication (also called matrix product) computes each result element as the dot product of a row and column. Element-wise multiplication (called the Hadamard product, denoted A ⊙ B) simply multiplies corresponding elements: C(i,j) = A(i,j) × B(i,j). The Hadamard product requires both matrices to have the same dimensions, while standard matrix multiplication requires the inner dimensions to match. They produce entirely different results.