Matrix Multiplication Calculator
Multiply two matrices together with step-by-step dot products.
Matrix Dimensions
B rows are locked to A columns (2), required for multiplication.
Matrix A (2×2)
Matrix B (2×2)
What Is the Matrix Multiplication Calculator?
The Matrix Multiplication Calculator multiplies two matrices of variable dimensions (2–3 rows and columns) and displays the full dot product breakdown for every entry in the result matrix. It enforces the fundamental compatibility rule, B's row count must match A's column count, automatically.
- ›Variable dimensions: A rows (2–3), A cols (2–3), B cols (2–3), B rows are locked to A cols automatically.
- ›Visual matrix grids: both A and B are displayed in bracket-styled grid inputs side by side with the × operator.
- ›Full dot product breakdown: every C[i][j] entry shows the individual products that sum to it, ideal for learning and checking by hand.
- ›Visual result matrix: C is displayed in the same bracket notation as the inputs.
- ›localStorage persistence: all dimensions and values are saved between sessions.
- ›Dimension change resets: changing any dimension clears affected grids to prevent stale incompatible data.
Formula
| Condition | Rule | Example |
|---|---|---|
| Compatibility | A must be m×k, B must be k×n (inner dimensions match) | A:3×2 × B:2×4 → C:3×4 |
| Result size | C is always m×n | 3×2 × 2×4 → 3×4 result |
| Commutativity | AB ≠ BA in general | Order matters always |
| Associativity | (AB)C = A(BC) | Grouping is flexible |
| Identity | A × I = I × A = A | I is n×n identity |
How to Use
- 1Set matrix A's dimensions using the row and column dropdowns (2 or 3 for each).
- 2Set matrix B's column count, B's rows are automatically locked to match A's columns.
- 3The result size is shown automatically: A rows × B columns.
- 4Enter values in the matrix A grid (left) and matrix B grid (right).
- 5Press Calculate (or Enter) to multiply the matrices.
- 6Read the result matrix C in bracket notation and review the full dot product breakdown below it.
- 7Press Clear to reset all cell values without changing dimensions.
Example Calculation
Example 1, 2×2 × 2×2
Example 2, 2×3 × 3×2 (non-square)
AB ≠ BA, order matters
Understanding Matrix Multiplication
Why Matrix Multiplication Works This Way
Matrix multiplication is defined the way it is because it corresponds to composing linear transformations. If matrix B transforms a vector from space V to space W, and matrix A transforms from space W to space X, then AB transforms directly from V to X, applying B first, then A.
The entry C[i][j] answers the question: "How much does the j-th basis vector of the input contribute to the i-th basis vector of the output?" This is exactly the dot product of row i (how A responds to inputs) with column j (what B does to the j-th input).
The Dimension Compatibility Rule
For A (m×k) × B (k×n) to be defined, A's column count must equal B's row count, both must equal k. This rule reflects the fact that the "output space" of B must match the "input space" of A.
- ›2×3 × 3×4 = 2×4 ✓ (inner dimension 3 matches)
- ›3×2 × 2×3 = 3×3 ✓ (inner dimension 2 matches)
- ›2×3 × 2×3 = undefined ✗ (inner dimensions 3 ≠ 2)
- ›3×1 × 1×3 = 3×3 ✓ (outer product, creates a matrix from two vectors)
- ›1×3 × 3×1 = 1×1 ✓ (dot product, creates a scalar from two vectors)
Non-Commutativity and Its Consequences
Unlike scalar multiplication, AB ≠ BA in general. In some special cases they are equal (e.g., if one matrix is a scalar multiple of I, or if both are diagonal), but this is the exception, not the rule. This non-commutativity has profound consequences:
- ›3D rotations: rotating 90° around X then 90° around Y gives a different result than Y then X, the order determines the final orientation.
- ›Quantum mechanics: observable operators (position, momentum) do not commute, [x̂, p̂] = iℏ, which leads directly to the Heisenberg uncertainty principle.
- ›Computer graphics pipelines: the order of scale, rotation, and translation transformations is critical, wrong order produces incorrect positioning.
Applications of Matrix Multiplication
- ›3D graphics: each object's position, rotation, and scale are encoded as 4×4 transformation matrices; the GPU multiplies them every frame for every vertex.
- ›Neural networks: forward pass = repeated matrix multiplications between weight matrices and activation vectors across layers.
- ›Graph theory: Aⁿ (matrix to the nth power) gives the number of length-n paths between every pair of nodes in a graph.
- ›Signal processing: DFT (discrete Fourier transform) is a matrix multiplication; FFT computes it efficiently by exploiting the matrix's structure.
- ›Economics: Leontief input-output models compute how production in one sector drives demand in others via matrix multiplication.
Frequently Asked Questions
Why do the inner dimensions of two matrices need to match for multiplication?
Each row of A has k entries, and each column of B has k entries. The dot product C[i][j] = sum of k products, one for each matching position. If A has 3 columns but B has only 2 rows, there is no way to pair up the elements, the operation is simply undefined.
Geometrically: if A maps vectors from a 3D space to a 2D space, and B maps vectors from a 2D space to a 4D space, then AB makes sense, B first reduces to 2D, then A processes that 2D output. But if B outputs 3D and A expects 2D input, they are incompatible.
- ›A: m×k maps from k-dimensional to m-dimensional space
- ›B: k×n maps from n-dimensional to k-dimensional space
- ›AB: applies B first (n→k), then A (k→m) → net result is n→m, i.e., A is m×n
Is matrix multiplication commutative?
No, in general, AB ≠ BA. This is one of the most fundamental differences between matrix algebra and scalar arithmetic.
AB may not even be the same size as BA: if A is 2×3 and B is 3×2, then AB is 2×2 but BA is 3×3, different sizes cannot possibly be equal.
Even for square matrices of the same size where both AB and BA are defined and the same size, they are almost always different:
- ›A=[[1,2],[3,4]], B=[[5,6],[7,8]]: AB=[[19,22],[43,50]], BA=[[23,34],[31,46]]
- ›Exception: diagonal matrices always commute with each other
- ›Exception: any matrix commutes with the identity I and with scalar multiples of I
- ›Exception: A always commutes with its own powers Aⁿ
What is a dot product and how does it relate to matrix multiplication?
The dot product of two vectors a = [a₁, a₂, …, aₙ] and b = [b₁, b₂, …, bₙ] is: a · b = a₁b₁ + a₂b₂ + … + aₙbₙ, a single number summarising how much the vectors "align."
Matrix multiplication is simply a systematic collection of dot products: C[i][j] is the dot product of row i of A with column j of B. An m×k times k×n multiplication computes m×n dot products total.
- ›If vectors are perpendicular, their dot product is 0
- ›If parallel and same direction, dot product = product of magnitudes
- ›Dot product = |a||b|cos(θ), where θ is the angle between them
What is the identity matrix and what happens when you multiply by it?
The identity matrix Iₙ is the n×n matrix with 1s on the main diagonal and 0s everywhere else. It is the matrix equivalent of the number 1, multiplying by it leaves any matrix unchanged.
- ›A × I = A for any compatible A
- ›I × A = A for any compatible A
- ›I₂ = [[1,0],[0,1]], I₃ = [[1,0,0],[0,1,0],[0,0,1]]
- ›det(I) = 1, and I⁻¹ = I
The identity matrix represents the "do nothing" transformation, no rotation, no scaling, no shearing. It is the starting point for building transformation matrices: you start with I and add rotation, scale, and translation components.
How does matrix multiplication represent linear transformations?
A matrix A represents a linear transformation: given any input vector x, the output is Ax. The columns of A are the images of the standard basis vectors, they tell you exactly where the transformation sends each axis direction.
- ›Rotation by θ: [[cos θ, −sin θ], [sin θ, cos θ]]
- ›Scaling by (sx, sy): [[sx, 0], [0, sy]]
- ›Reflection across x-axis: [[1, 0], [0, −1]]
- ›Shear: [[1, k], [0, 1]], slides horizontal layers
Composing two transformations (first B, then A) is equivalent to computing the matrix product AB. The order matters: AB means "apply B first, then A." In 3D graphics, a model matrix M = T × R × S means "scale first (S), then rotate (R), then translate (T)."
What is the computational cost of matrix multiplication?
For standard matrix multiplication, multiplying an m×k matrix by a k×n matrix requires m×k×n scalar multiplications and m×k×(k−1) additions, approximately O(m·k·n) operations total.
- ›2×2 × 2×2: 8 multiplications, 4 additions
- ›3×3 × 3×3: 27 multiplications, 18 additions
- ›100×100 × 100×100: 1,000,000 multiplications
- ›1000×1000 × 1000×1000: 10⁹ operations (CPU-intensive)
For large matrices, algorithms like Strassen's (O(n^2.807)) and Coppersmith-Winograd reduce theoretical complexity. In practice, highly optimised BLAS libraries (LAPACK, cuBLAS) exploit CPU SIMD instructions and GPU parallelism to multiply large matrices orders of magnitude faster than naive implementations.
What does Aⁿ (matrix power) represent and how is it calculated?
Aⁿ means applying the transformation represented by A exactly n times. It is computed by repeated matrix multiplication: A² = A×A, A³ = A²×A, and so on. Efficient computation uses repeated squaring, O(log n) multiplications instead of O(n).
- ›Rotation matrix Rθ to the power n → rotation by nθ
- ›Adjacency matrix A of a graph: A^n gives the number of length-n paths between each pair of nodes
- ›Markov transition matrix P^n → probability distribution after n steps
- ›Fibonacci sequence: [[1,1],[1,0]]^n gives F(n) and F(n+1) in O(log n) multiplications
Matrix diagonalisation simplifies computing Aⁿ: if A = PDP⁻¹ (where D is diagonal), then Aⁿ = PDⁿP⁻¹, and Dⁿ is trivial (just raise each diagonal entry to the nth power). This technique is used in differential equations to find closed-form solutions for systems of equations.