Matrices and Matrix Operations

Matrices and Matrix Operations

Definition: A matrix is a rectangular array of numbers arranged in rows and columns, written as an m×n matrix when it has m rows and n columns; matrices can be added, scaled, and multiplied as single objects, and every 2×2 matrix doubles as a description of a linear transformation of the plane.

How It Works

  • A matrix is a rectangular array of numbers arranged in rows and columns; its dimensions are written rows × columns, so a matrix with 3 rows and 2 columns is a 3×2 matrix, and each individual number is called an entry, written a_ij for the entry in row i, column j.
  • Matrix addition and subtraction are element-wise: add or subtract each entry to whichever entry sits in the same position in the other matrix. This only works when both matrices share identical dimensions; a 2×3 matrix cannot be added to a 3×2 one.
  • Scalar multiplication multiplies every entry in a matrix by the same single number (the scalar), scaling the whole array uniformly without changing its dimensions.
  • Matrix multiplication is not element-wise. Entry (i, j) of the product is the dot product of row i from the first matrix and column j from the second: multiply corresponding entries together, then add the results.
  • Two matrices can only be multiplied when the first matrix’s column count equals the second matrix’s row count: an (m×n) matrix times an (n×p) matrix produces an (m×p) result. Every output entry needs a row and a column of matching length to pair up; without matching lengths there is nothing to pair, so the product is simply undefined.
  • The identity matrix (I) is the matrix equivalent of the number 1: a square matrix with 1s down the main diagonal and 0s everywhere else. Multiplying any matrix by the appropriately-sized identity leaves it completely unchanged.
  • The determinant of a 2×2 matrix [[a, b], [c, d]] is the single number ad - bc, computed directly from its four entries.
  • A determinant of zero means the matrix is singular (non-invertible): no matrix can undo its transformation, because the transformation has already destroyed information by collapsing the plane down onto a line or a single point.
  • Every 2×2 matrix can be read as a linear transformation of the plane: it takes every point (x, y) and moves it to a new location, and remarkably, the entire transformation is fully pinned down by just two facts, where it sends the point (1, 0) and where it sends the point (0, 1).
  • Those two points, (1, 0) and (0, 1), are the standard basis vectors, and every other point in the plane is some combination of them; the matrix’s first column is exactly where (1, 0) lands, and its second column is exactly where (0, 1) lands.
  • Because a linear transformation always keeps the origin fixed and keeps straight lines straight, applying a matrix to a grid never bends or curves it: grid lines stay straight and evenly spaced, only their direction, spacing, and angles change.

Illustration

x y -4 4 -4 4

Matrix: [[1.0, 0.0], [0.0, 1.0]] Determinant: 1.0 Area scale: 1.0×

Drag the sliders below to change the matrix entries m11, m12, m21, m22. The dashed square is the original unit square; the solid square is its live image after the transformation, redrawing together with the matrix, determinant, and area-scale readouts.

var m11In = document.getElementById(‘mat-m11’); var m12In = document.getElementById(‘mat-m12’); var m21In = document.getElementById(‘mat-m21’); var m22In = document.getElementById(‘mat-m22’); var m11Out = document.getElementById(‘mat-m11-out’); var m12Out = document.getElementById(‘mat-m12-out’); var m21Out = document.getElementById(‘mat-m21-out’); var m22Out = document.getElementById(‘mat-m22-out’); var resetBtn = document.getElementById(‘mat-reset’);

function fmt(n) { return (Math.round(n * 100) / 100).toFixed(2).replace(/.00$/, ‘.0’); }

function update() { var m11 = parseFloat(m11In.value); var m12 = parseFloat(m12In.value); var m21 = parseFloat(m21In.value); var m22 = parseFloat(m22In.value); m11Out.textContent = fmt(m11); m12Out.textContent = fmt(m12); m21Out.textContent = fmt(m21); m22Out.textContent = fmt(m22);

// Apply the matrix to each corner of the unit square, using the
// standard matrix-vector rule for [[m11,m12],[m21,m22]] acting on (x,y):
// transformed_x = m11*x + m12*y, transformed_y = m21*x + m22*y
var corners = [[0, 0], [1, 0], [1, 1], [0, 1]];
var pts = [];
for (var i = 0; i < corners.length; i++) {
  var x = corners[i][0], y = corners[i][1];
  var tx = m11 * x + m12 * y;
  var ty = m21 * x + m22 * y;
  var p = toPx(tx, ty);
  pts.push(p[0].toFixed(1) + ',' + p[1].toFixed(1));
}
poly.setAttribute('points', pts.join(' '));

var det = m11 * m22 - m12 * m21;
matrixTxt.textContent = 'Matrix: [[' + fmt(m11) + ', ' + fmt(m12) + '], [' + fmt(m21) + ', ' + fmt(m22) + ']]';
detTxt.textContent = 'Determinant: ' + fmt(det);

if (Math.abs(det) < 0.01) {
  noteTxt.textContent = 'Singular! This transformation collapses the square to a line.';
} else {
  noteTxt.textContent = 'Area scale: ' + fmt(Math.abs(det)) + '×';
}

}

[m11In, m12In, m21In, m22In].forEach(function (el) { el.addEventListener(‘input’, update); }); resetBtn.addEventListener(‘click’, function () { m11In.value = 1; m12In.value = 0; m21In.value = 0; m22In.value = 1; update(); });

update(); })();

Under the Hood

Matrix multiplication, precisely, worked out on a real 2×2 example:

For A (m×n) and B (n×p), the product C = AB is an (m×p) matrix, where every
entry is a dot product of a row from A and a column from B:
C_ij = (row i of A) · (column j of B) = A_i1·B_1j + A_i2·B_2j + ... + A_in·B_nj

Example, multiplying a 2×2 by a 2×2:
A = [1 2]     B = [5 6]     C11 = 1·5 + 2·7 = 19     C12 = 1·6 + 2·8 = 22
    [3 4]         [7 8]     C21 = 3·5 + 4·7 = 43     C22 = 3·6 + 4·8 = 50

AB = [19 22]
     [43 50]

The determinant of a 2×2 matrix is just as direct:

det [a b] = ad - bc
    [c d]

Worked Example 1: Multiplying two 2×2 matrices. Given: A = [[2, 0], [1, 3]], B = [[4, 1], [-2, 5]]. Step 1: C11 = row 1 of A · column 1 of B = (2)(4) + (0)(-2) = 8. Step 2: C12 = row 1 of A · column 2 of B = (2)(1) + (0)(5) = 2. Step 3: C21 = row 2 of A · column 1 of B = (1)(4) + (3)(-2) = -2. Step 4: C22 = row 2 of A · column 2 of B = (1)(1) + (3)(5) = 16. Answer: AB = [[8, 2], [-2, 16]].

Worked Example 2: Computing a determinant and interpreting it. Given: M = [[3, 1], [6, 2]]. Step 1: det(M) = (3)(2) - (1)(6) = 6 - 6 = 0. Answer: det(M) = 0, so M is singular. Its columns, (3, 6) and (1, 2), point in exactly the same direction (one is just 3 times the other), so M squashes the entire plane down onto that single line, and no inverse exists that could undo that loss of information.

Worked Example 3: Rotating a point with a rotation matrix. Given: rotate the point (2, 0) by 90 degrees counterclockwise using R = [[cos 90°, -sin 90°], [sin 90°, cos 90°]] = [[0, -1], [1, 0]]. Step 1: transformed_x = m11·x + m12·y = (0)(2) + (-1)(0) = 0. Step 2: transformed_y = m21·x + m22·y = (1)(2) + (0)(0) = 2. Answer: (2, 0) rotates to (0, 2), exactly a quarter-turn counterclockwise onto the positive y-axis, at the same distance 2 from the origin.

History

  • Matrix-like arrays of numbers were used implicitly over 2,000 years ago in the ancient Chinese text The Nine Chapters on the Mathematical Art, which describes solving systems of linear equations using counting rods arranged in a grid on a board, in essence the same row operations a matrix supports today.
  • The formal term “matrix” was coined by the English mathematician James Joseph Sylvester in 1850, borrowing the Latin word for “womb,” to describe an array from which determinants could be generated.
  • Arthur Cayley developed matrix algebra into its own systematic subject through the 1850s, publishing “A Memoir on the Theory of Matrices” in 1858, where he formally defined matrix addition, multiplication, and inverses.
  • Matrices became central to 20th-century physics when Werner Heisenberg formulated quantum mechanics in 1925 using matrices to represent physical quantities, an approach known as matrix mechanics.
  • The rise of digital computers in the mid-20th century turned large matrix calculations from a slow, hand-worked chore into something a machine could execute millions of times per second, unlocking practical uses at a massive scale.
  • Matrices are now foundational to computer graphics, machine learning, and data science, where transformations, datasets, and neural network weights are all represented and manipulated as matrices.

Why It Matters

  • Computer graphics represent every 2D and 3D transformation, rotation, scaling, and (via an extra coordinate) translation, as matrix multiplication, so an entire chain of transformations reduces to a single combined matrix applied once.
  • Machine learning and neural networks are fundamentally built from matrix multiplications: each layer of a network multiplies its inputs by a matrix of weights, repeated across millions or billions of parameters.
  • Solving systems of linear equations efficiently at large scale, thousands or millions of unknowns at once, relies on matrix methods rather than solving one equation at a time.
  • Quantum mechanics represents physical states as vectors and measurable quantities as matrices, so predicting how a quantum system evolves is fundamentally a matrix computation.
  • Economists build input-output models entirely from matrices to track how output from one industry feeds in as input to every other industry across a whole economy.
  • Search engines rank pages by representing the entire web of links as one enormous matrix and running matrix operations on it to find which pages matter most.

Common Pitfalls

  • Assuming matrix multiplication is commutative. In general AB ≠ BA, even when both products are defined, so the order of multiplication cannot be swapped freely the way it can with ordinary numbers.
  • Trying to multiply two matrices with incompatible dimensions. If the first matrix’s column count does not match the second matrix’s row count, the product is not “wrong,” it is undefined; there is no result to compute.
  • Confusing element-wise multiplication (multiplying matching entries directly) with true matrix multiplication (the row-by-column dot-product rule). They are different operations that almost always give different answers.
  • Forgetting the determinant test for invertibility. A matrix with a zero determinant has no inverse at all, no matter how ordinary its individual entries look.
  • Applying the 2×2 determinant shortcut, ad - bc, to matrices that are not 2×2. Determinants are only defined for square matrices, and larger square matrices need a more involved formula, not this shortcut.
  • Mixing up the row and column in the entry notation a_ij, forgetting it means row i, column j. Swapping that silently transposes every calculation that follows.

Comparison

OperationDimension RequirementCommutative?Rule for Each Entry
Addition / SubtractionBoth matrices must be the exact same size (m×n)Yes: A + B = B + AC_ij = A_ij ± B_ij (add or subtract matching entries)
MultiplicationInner dimensions must match: (m×n) times (n×p)No: AB ≠ BA in generalC_ij = (row i of A) · (column j of B), a dot product

FAQ

Why isn’t matrix multiplication just element-by-element, the way addition is? Because matrices represent linear transformations, and multiplying two matrices needs to correspond to applying one transformation after another. Multiplying entry-by-entry would not have that property; the row-by-column dot-product rule is defined specifically so that matrix multiplication matches transformation composition. (An element-wise product does exist and is occasionally useful, called the Hadamard product, but it is not what “matrix multiplication” means by default.)

Does every matrix have an inverse? No. Only square matrices can have an inverse, and even then only if the determinant is nonzero. A singular matrix has already collapsed some information, flattening part of the plane down to a line or a point, and no operation can recover information that has been thrown away.

Can matrices of different sizes still be multiplied together? Yes, as long as the inner dimensions match. A 2×3 matrix times a 3×1 matrix is perfectly valid and produces a 2×1 result, even though the outer dimensions, 2 and 1, differ from each other.

Example

A video game engine spinning a spaceship sprite by 30 degrees every frame does it by multiplying every point of the sprite’s outline by the same small matrix, [[cos 30°, -sin 30°], [sin 30°, cos 30°]] ≈ [[0.87, -0.50], [0.50, 0.87]], the exact operation the widget above performs live whenever its sliders are set to those four numbers. Because rotation matrices always have determinant exactly 1 (cos²θ + sin²θ = 1, always), the sprite’s size and area never change, only its orientation, proof that a single 2×2 grid of numbers is enough to completely describe a rigid spin of an entire shape, point by point.

Dig deeper