Prerequisite · Matrix Algebra Foundations

Matrix Operations

16 min read
0:00
Audio overview generated with
By the end of this reading you will be able to:
  • Compute the transpose of a matrix and apply the property (AB)^T = B^T A^T
  • Execute matrix addition, scalar multiplication, and matrix multiplication, checking conformability before each product
  • Calculate an inner product as a dot product and interpret the result geometrically as a similarity score
  • Explain why matrix multiplication is not commutative and give a concrete counterexample
  • Interpret a matrix-vector product as a linear combination of the matrix columns, and connect this view to how a neural network layer transforms its input

Equality

Two matrices AA and BB are equal if and only if they have the same dimensions and every corresponding element is equal:

A=B    aij=bij for all i,jA = B \iff a_{ij} = b_{ij} \text{ for all } i, j

Dimension mismatch means not-equal regardless of element values. This seems obvious but matters when checking whether a transformation preserves matrix structure.

Transposition

The transpose of AA, written AA' or AA^\top, is formed by swapping rows and columns — the kk-th row of AA becomes the kk-th column of AA':

B=A    bij=ajiB = A' \iff b_{ij} = a_{ji}

If AA is n×Kn \times K, then AA' is K×nK \times n.

Properties of the Transpose

  • (A)=A(A')' = A — transposing twice recovers the original
  • (A+B)=A+B(A + B)' = A' + B' — transpose distributes over addition
  • (AB)=BA(AB)' = B'A' — transpose reverses the order of multiplication
  • (ABC)=CBA(ABC)' = C'B'A' — reversal extends to any product length
  • AA is symmetric     A=A\iff A = A'

The reversal rule (AB)=BA(AB)' = B'A' catches many beginners off guard. It arises from the way inner products work: transposing a product swaps which matrix is pre- and post-multiplied.

Matrix Addition and Subtraction

Addition and subtraction are element-wise operations:

C=A+B=[aij+bij]C=AB=[aijbij]C = A + B = [a_{ij} + b_{ij}] \qquad C = A - B = [a_{ij} - b_{ij}]

Matrices must have identical dimensions to be added — they are then said to be conformable for addition.

Properties

  • Commutative: A+B=B+AA + B = B + A
  • Associative: (A+B)+C=A+(B+C)(A + B) + C = A + (B + C)
  • Additive identity: A+0=AA + \mathbf{0} = A

Scalar Multiplication

Multiplying a matrix by a scalar cc scales every element:

cA=[caij]cA = [c \cdot a_{ij}]

Scalar multiplication commutes: cA=AccA = Ac. It distributes over addition: c(A+B)=cA+cBc(A+B) = cA + cB.

Vector Inner Product

The inner product (dot product) of two column vectors a\mathbf{a} and b\mathbf{b} of the same length is a scalar:

ab=a1b1+a2b2++anbn=i=1naibi\mathbf{a}'\mathbf{b} = a_1 b_1 + a_2 b_2 + \cdots + a_n b_n = \sum_{i=1}^n a_i b_i

Note the form: ab\mathbf{a}'\mathbf{b} is a row vector times a column vector, yielding a 1×11 \times 1 scalar. Since each term aibi=biaia_i b_i = b_i a_i, the inner product is symmetric:

ab=ba\mathbf{a}'\mathbf{b} = \mathbf{b}'\mathbf{a}

Geometrically, ab=abcosθ\mathbf{a}'\mathbf{b} = \|\mathbf{a}\|\,\|\mathbf{b}\|\cos\theta where θ\theta is the angle between the vectors. The inner product is zero when vectors are orthogonal — a fundamental concept in least squares and PCA.

Matrix Multiplication

For an n×Kn \times K matrix AA and a K×MK \times M matrix BB, the product C=ABC = AB is n×Mn \times M, where element cikc_{ik} is the inner product of row ii of AA with column kk of BB:

C=AB,cik=aibk=j=1KaijbjkC = AB, \qquad c_{ik} = \mathbf{a}_i' \mathbf{b}_{\cdot k} = \sum_{j=1}^K a_{ij}\, b_{jk}

Conformability

The matrices must be conformable for multiplication: the number of columns in AA must equal the number of rows in BB. A useful check: write the dimensions in sequence — (n×K)(K×M)(n \times K)(K \times M) — the inner dimensions must match, and the outer dimensions give the result.

Non-Commutativity

Matrix multiplication is generally not commutative:

ABBA in generalAB \neq BA \text{ in general}

In fact, BABA may not even be defined (if dimensions don't conform), or it may have different dimensions than ABAB, or it may be defined with the same dimensions but unequal to ABAB. This is why we distinguish pre-multiplication (BB is premultiplied by AA) from post-multiplication.

Properties

  • Associative: (AB)C=A(BC)(AB)C = A(BC)
  • Distributive: A(B+C)=AB+ACA(B + C) = AB + AC
  • Identity: AI=IA=AAI = IA = A
  • Zero matrix: A0=0A\mathbf{0} = \mathbf{0}
  • Transpose of product: (AB)=BA(AB)' = B'A'

Column Interpretation

Each column of C=ABC = AB is a linear combination of the columns of AA, with coefficients drawn from the corresponding column of BB:

ck=Abk=b1ka1+b2ka2++bKkaK\mathbf{c}_{\cdot k} = A\, \mathbf{b}_{\cdot k} = b_{1k}\,\mathbf{a}_{\cdot 1} + b_{2k}\,\mathbf{a}_{\cdot 2} + \cdots + b_{Kk}\,\mathbf{a}_{\cdot K}

This column interpretation is one of the most useful ways to think about what a matrix multiplication does: it expresses the columns of the result as combinations of the columns of the left factor.

Sums via Vectors

A column of ones i=[1,1,,1]\mathbf{i} = [1, 1, \ldots, 1]' (length nn) provides compact notation for sums:

i=1nxi=ix\sum_{i=1}^n x_i = \mathbf{i}'\mathbf{x}

The arithmetic mean follows immediately:

xˉ=1nix=ixn\bar{x} = \frac{1}{n}\mathbf{i}'\mathbf{x} = \frac{\mathbf{i}'\mathbf{x}}{n}

The sum of squares and cross-products of a data matrix XX (n×Kn \times K) take the matrix form XXX'X, where element [XX]jk[X'X]_{jk} is the inner product of columns jj and kk of XX. This K×KK \times K matrix is the workhorse of ordinary least squares.

References
Greene 2003 — Econometric Analysis, 5th ed., Appendix A.2