Skip to content
AI360Xpert
Beta

Matrix Operations

Matrices add entry by entry, multiply row by column with inner sizes that must match, and transpose flips rows into columns.

Two 2 by 2 matrices multiply row by column: row 1 of A meets both columns of B to make 19 and 22, row 2 makes 43 and 50, and only matching inner sizes let the product exist
Two 2 by 2 matrices multiply row by column: row 1 of A meets both columns of B to make 19 and 22, row 2 makes 43 and 50, and only matching inner sizes let the product exist

Why Does This Exist?

Every layer of a neural network is a matrix operation. A dense layer multiplies a batch of inputs by a weight matrix, adds a bias, and passes the result on. Data preprocessing centers matrices, attention scores them against their transposes, and gradients flow back through the same three operations. If adding, multiplying, and transposing feel shaky, nothing downstream can feel solid.

Vectors, matrices, and tensors introduces what these objects are. This page is the verbs: the three operations, their shape rules, and the one order trap that bites everyone.

The one idea you need first

A matrix shape like 2×32 \times 3 reads rows first: 2 rows, 3 columns. Entry aija_{ij} sits in row ii, column jj. Nearly every error in this subject is a shape error, so read shapes before computing anything.

Think of It Like This

Recipes scaled across kitchens

Think of a matrix as a recipe card: rows are dishes, columns are ingredients. Adding two cards blends two menus dish by dish. Multiplying is fulfilling orders: each row of the first card (a dish's needs) meets each column of the second (a supplier's offering) to produce a table of total costs. Transposing rewrites the card sideways, ingredients as rows, so tomorrow's computation reads naturally.

The analogy's limit is deliberate: real matrices multiply numbers, not intentions, and the row-meets-column rule has no flexibility. A dish with 3 ingredient entries can only meet a supplier listing 3 dishes, which is the inner-size rule with an apron on.

How It Actually Works

Addition: entry by entry, same shape required. Add matching positions:

[12 34]+[56 78]=[68 1012]\begin{bmatrix} 1 & 2 \ 3 & 4 \end{bmatrix} + \begin{bmatrix} 5 & 6 \ 7 & 8 \end{bmatrix} = \begin{bmatrix} 6 & 8 \ 10 & 12 \end{bmatrix}

A 2×22 \times 2 plus a 2×32 \times 3 is undefined, full stop. (Broadcasting in libraries bends this rule; the math does not.)

Multiplication: row by column, inner sizes must match. For C=ABC = AB, entry cijc_{ij} is row ii of AA dotted with column jj of BB. An (m×n)(m \times n) times an (n×p)(n \times p) gives (m×p)(m \times p): the inner nns must agree and the outer sizes survive.

Worked example

A=[12 34],B=[56 78]A = \begin{bmatrix} 1 & 2 \ 3 & 4 \end{bmatrix}, \qquad B = \begin{bmatrix} 5 & 6 \ 7 & 8 \end{bmatrix}

Both are 2×22 \times 2, so the product is 2×22 \times 2:

  • c11=1×5+2×7=5+14=19c_{11} = 1 \times 5 + 2 \times 7 = 5 + 14 = 19
  • c12=1×6+2×8=6+16=22c_{12} = 1 \times 6 + 2 \times 8 = 6 + 16 = 22
  • c21=3×5+4×7=15+28=43c_{21} = 3 \times 5 + 4 \times 7 = 15 + 28 = 43
  • c22=3×6+4×8=18+32=50c_{22} = 3 \times 6 + 4 \times 8 = 18 + 32 = 50

So AB=[1922 4350]AB = \begin{bmatrix} 19 & 22 \ 43 & 50 \end{bmatrix}. Reversing the order gives BA=[2334 3146]BA = \begin{bmatrix} 23 & 34 \ 31 & 46 \end{bmatrix}, a different matrix, which proves the headline warning: AB≠BAAB \neq BA in general.

Transpose: flip across the diagonal. Rows become columns, so an (m×n)(m \times n) becomes (n×m)(n \times m):

AT=[13 24]A^T = \begin{bmatrix} 1 & 3 \ 2 & 4 \end{bmatrix}

Entry (i,j)(i, j) moves to (j,i)(j, i). Transposing twice returns the original: (AT)T=A(A^T)^T = A.

Watch Out For

Multiplying matrices whose inner sizes disagree

The symptom is a shape error at 1am: a (4×3)(4 \times 3) weight meeting a (4×1)(4 \times 1) input and refusing to multiply. The fix is the outer-inner chant: write both shapes down, check the middle numbers match, and read the answer shape off the outer numbers. Nine times out of ten the fix is a missing transpose on one side, which is exactly why transpose exists.

Assuming AB equals BA

Scalar habits die hard, and matrix multiplication is not commutative. The symptom is a silently wrong result, because BABA often has a valid shape and computes without complaint, as the worked example shows with two fully valid but different answers. Gradients, rotations, and projections all depend on the order. When porting a formula, preserve the order exactly; when debugging, swap suspects deliberately and compare.

The Quick Version

  • Addition is entry by entry and needs identical shapes.
  • Multiplication is row by column: (m×n)(m \times n) times (n×p)(n \times p) gives (m×p)(m \times p).
  • The worked product of the two example matrices is [1922 4350]\begin{bmatrix} 19 & 22 \ 43 & 50 \end{bmatrix}.
  • Transpose flips rows and columns: entry (i,j)(i,j) moves to (j,i)(j,i).
  • AB≠BAAB \neq BA in general, so operand order is part of the formula.
  • When shapes refuse to multiply, check the inner sizes, then suspect a missing transpose.