Lesson 5 of 9 · Linear Algebra

Matrix transpose

The transpose swaps rows and columns; we look at its properties, the rule for the transpose of a product, and its role in writing the dot product, the Gram matrix and backpropagation.

Mathematics for machine learning Difficulty: Beginner Study time: 30 min
Lesson contents
  1. Introduction
  2. Why study this topic?
  3. Theoretical explanation
  4. Mathematical definitions
  5. The equations
  6. Worked example
  7. Application in AI
  8. Interactive lab
  9. Practice exercises
  10. Summary
  11. Short quiz
  12. Related lessons

1 Introduction

The matrix transpose is a simple operation: the first row becomes the first column, and so on. Yet it appears in almost every equation of machine learning.

Learning objectives

  • Computing the transpose of a matrix
  • Applying the rule \((AB)^\top = B^\top A^\top\)
  • Recognizing symmetric matrices and the Gram matrix

2 Why study this topic?

The dot product is written \(\mathbf{a}^\top\mathbf{b}\), the normal equations of linear regression contain \(X^\top X\), and the weight gradient in backpropagation is computed by multiplying by the transpose \(W^\top\). Likewise, the score matrix in attention is \(QK^\top\).

3 Theoretical explanation

Definition

If \(A\in\mathbb{R}^{m\times n}\), its transpose is \(A^\top\in\mathbb{R}^{n\times m}\) where \((A^\top)_{ij} = a_{ji}\). A column vector becomes a row when transposed.

Properties

  • \((A^\top)^\top = A\)
  • \((A+B)^\top = A^\top + B^\top\)
  • \((cA)^\top = cA^\top\)
  • \((AB)^\top = B^\top A^\top\) — note that the order is reversed.

Gram matrix

For any matrix \(X\), the matrix \(X^\top X\) is square and symmetric, because \((X^\top X)^\top = X^\top X\). Its elements are dot products between the columns of \(X\), and it appears in linear regression and in computing the covariance.

4 Mathematical definitions

Transpose
The matrix obtained by swapping rows and columns.
\[(A^\top)_{ij} = a_{ji}\]
Gram matrix
The product of a matrix's transpose with the matrix itself; it is always symmetric.
\[G = X^\top X\]
Transpose
The matrix obtained by swapping rows and columns.
\[(A^\top)_{ij}=a_{ji}\]

5 The equations

\[(AB)^\top = B^\top A^\top\]
Transpose of a product
\[\mathbf{a}\cdot\mathbf{b} = \mathbf{a}^\top\mathbf{b}\]
The dot product via the transpose
\[\hat{\mathbf{w}} = (X^\top X)^{-1}X^\top\mathbf{y}\]
The normal equations of linear regression

6 Step-by-step worked example

A matrix transpose and the Gram matrix

Problem: Let \(X = \begin{bmatrix}1 & 2 \\ 0 & 1 \\ 3 & -1\end{bmatrix}\). Compute \(X^\top\) and \(X^\top X\).

  1. The transpose: \(X^\top = \begin{bmatrix}1 & 0 & 3 \\ 2 & 1 & -1\end{bmatrix}\) (its shape is 2×3).
  2. We multiply \((2\times3)(3\times2)\) and get 2×2.
  3. Element (1,1): \(1+0+9 = 10\), element (1,2): \(2+0-3=-1\), and element (2,2): \(4+1+1=6\).

Result: \(X^\top X = \begin{bmatrix}10 & -1 \\ -1 & 6\end{bmatrix}\), which is symmetric as expected.

7 Practical application in AI

  • Linear regression: The closed-form solution \((X^\top X)^{-1}X^\top\mathbf{y}\) relies on the transpose.
  • Backpropagation: If \(\mathbf{z}=W\mathbf{x}\), the gradient of the loss with respect to \(\mathbf{x}\) is \(W^\top\,\partial L/\partial\mathbf{z}\).
  • Attention: The attention scores \(QK^\top/\sqrt{d_k}\) use the transpose of the key matrix.

8 Interactive lab linked to the lesson

Vector operations (addition, subtraction, dot product, length, distance and cosine similarity), matrix operations (addition, multiplication, transpose, determinant and inverse), eigenvalues and eigenvectors, and singular value decomposition (SVD) with lower-rank reconstruction.

Open the Linear Algebra Lab

9 Practice exercises

Review questions for reflection

  • Why is \(X^\top X\) square even when X is rectangular?
  • What is the transpose of a column vector of dimension 5?

Common mistakes

  • Writing \((AB)^\top = A^\top B^\top\) without reversing the order.
  • Forgetting that the shape of the transpose is reversed.

Practice 5 questions from the question bank for this lesson

10 Lesson summary

  • The transpose swaps rows and columns.
  • \((AB)^\top = B^\top A^\top\).
  • \(X^\top X\) is always symmetric.

11 Short quiz

5 questions · 10 minutes · pass mark 60% — passing it completes the lesson.