EasyLinear Algebra

Matrix Transpose

Linear Algebra

Easy

Problem

Implement the transpose of a matrix, where each element at position (i, j) is swapped to (j, i).

Mathematical Definition

Transpose Operation:

$$ (A^{T}){ji} = A{ij}

$$

An n×m matrix becomes an m×n matrix.

Function Arguments

  • A is a two-dimensional Python list of shape (N, M) representing the input matrix

Theory

The transpose of a matrix flips it over its diagonal. Every element at position (i, j) moves to position (j, i). Rows become columns and columns become rows.

If A is an n \times m matrix, then A^T is an m \times n matrix defined by:

(A^T)_{ij} = A_{ji}

This simple operation appears everywhere in linear algebra, statistics, and machine learning. Understanding it deeply pays off.


A Concrete Example

Consider the matrix:

A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}

This is a 2 \times 3 matrix (2 rows, 3 columns).

Its transpose is:

A^T = \begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix}

This is a 3 \times 2 matrix (3 rows, 2 columns).

Notice what happened:


The Transpose of a Vector

Vectors are special cases:

Column vector (shape n \times 1):

v = \begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix}

Its transpose is a row vector (shape 1 \times n):

v^T = \begin{bmatrix} 1 & 2 & 3 \end{bmatrix}

This is why you often see notation like x^T y for the dot product: it is a row vector times a column vector, which gives a scalar.


Key Properties of the Transpose

Double transpose returns the original:

(A^T)^T = A

Transpose of a sum:

(A + B)^T = A^T + B^T

Transpose of a product (order reverses!):

(AB)^T = B^T A^T

This reversal is crucial. If you have a chain of matrices, transposing reverses the order:

(ABC)^T = C^T B^T A^T

Transpose of a scalar multiple:

(cA)^T = c A^T


Special Matrices and Transpose

Symmetric matrices:

A matrix is symmetric if A^T = A. This means A_{ij} = A_{ji} for all entries. The matrix equals its mirror image across the diagonal.

Example:

\begin{bmatrix} 1 & 2 & 3 \\ 2 & 4 & 5 \\ 3 & 5 & 6 \end{bmatrix}

Symmetric matrices appear constantly in ML:

Skew-symmetric matrices:

A matrix is skew-symmetric if A^T = -A. The diagonal must be zero.

Example:

\begin{bmatrix} 0 & 2 & -3 \\ -2 & 0 & 5 \\ 3 & -5 & 0 \end{bmatrix}


Why Transpose Matters in Machine Learning

The Gram matrix X^T X:

If X is your data matrix with n samples and d features (shape n \times d), then:

The covariance structure:

The sample covariance matrix is proportional to X^T X (after centering). Understanding how transposes work is essential for deriving the normal equations, PCA, and more.

Backpropagation:

In neural networks, the gradient with respect to the input of a linear layer involves the transpose of the weight matrix. If the forward pass is y = Wx, the backward pass involves W^T.


Transpose and Inner Products

The dot product of two vectors can be written as:

x \cdot y = x^T y = \sum_i x_i y_i

This is a 1 \times n row vector times an n \times 1 column vector, giving a 1 \times 1 scalar.

The outer product is the transpose arrangement:

x y^T

This is an n \times 1 column vector times a 1 \times m row vector, giving an n \times m matrix. Each entry is x_i y_j.


Implementation Perspective

Transposing a matrix in code is straightforward but has performance implications:

In-place vs. copy:

Memory layout:

View vs. copy in NumPy:

Examples

Example 1

Input
A = [[1, 2, 3], [4, 5, 6]]
Output
[[1, 4], [2, 5], [3, 6]]

Example 2

Input
A = [[1, 2], [3, 4]]
Output
[[1, 3], [2, 4]]

Example 3

Input
A = [[1, 2, 3, 4]]
Output
[[1], [2], [3], [4]]

Hints

  1. Create an output array whose row and column counts are reversed.
  2. Inside nested loops, assign output[j, i] from A[i][j].

Requirements

Constraints

Starter Code

import numpy as np

def matrix_transpose(A: list) -> np.ndarray:
    """
    Returns the transposed matrix as a NumPy array.
    """
    # Write code here
    pass

Test Cases

CaseMatches
2x3 matrixExample 1public
2x2 matrixExample 2public
1x4 row vectorExample 3public