# Matrix

158pages on
this wiki

A matrix is a rectangular array of scalars or other kind of elements.

Matrices have many uses in computer graphics, some of which are representing linear transformations, affine transformations or images, among others.

## Definitions and notationsEdit

The horizontal lines in a matrix are called rows and the vertical lines are called columns. A matrix with m rows and n columns is called an m-by-n matrix (or m×n matrix) and m and n are called its dimensions. The dimensions of a matrix are always given with the number of rows first, then the number of columns.

The entry of a matrix A that lies in the i -th row and the j-th column is called the i,j entry or (i,j)-th entry of A. This is written as $a_{i,j}$ or A[i,j].

We often write $A:=(a_{i,j})_{m \times n}$ to define an m × n matrix A with each entry in the matrix A[i,j] called $a_{i,j}$ for all 1 ≤ im and 1 ≤ jn. However, the convention that the indices i and j start at 1 is not universal: some programming languages (and the pseudocode in this wiki) start at zero, in which case we have 0 ≤ i ≤ (m − 1) and 0 ≤ j ≤ (n − 1).

A matrix where one of the dimensions equals one is often called a vector. A 1 × n matrix (one row and n columns) is called a row vector, and an m × 1 matrix (one column and m rows) is called a column vector.

## ExampleEdit

The matrix

$A = \begin{bmatrix} 1 & 2 & 3 \\ 1 & 2 & 7 \\ 4 & 9 & 2 \\ 6 & 0 & 5 \end{bmatrix}$

is a 4×3 matrix. The element A[2,3] or a2,3 is 7.

The matrix

$R = \begin{bmatrix} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \end{bmatrix}$

is a 1×9 matrix, or 9-element row vector.

The matrix

$C = \begin{bmatrix} 2 \\ 7 \\ 0 \end{bmatrix}$

is a 3×1 matrix, or 3-element column vector

## Operations on matricesEdit

Given m-by-n matrices A and B, their sum A + B is the m-by-n matrix computed by adding corresponding elements (i.e. (A + B)[i, j] = A[i, j] + B[i, j] ). For example:

Note both A and B must have the same dimension.

$\begin{bmatrix} 1 & 3 & 2 \\ 1 & 0 & 0 \\ 1 & 2 & 2 \end{bmatrix} + \begin{bmatrix} -1 & 7 & 2 \\ 0 & 1 & 0 \\ -1 & 2 & 0 \end{bmatrix} = \begin{bmatrix} 1 + -1 & 3 + 7 & 2+2 \\ 1 + 0 & 0 + 1 & 0+0 \\ 1 + -1 & 2 + 2 & 2+0 \end{bmatrix} = \begin{bmatrix} 0 & 10 & 4 \\ 1 & 1 & 0 \\ 0 & 4 & 2 \end{bmatrix}$

pseudocode

Matrix  C[m][n], A[m][n], B[m][n]
integer i,j

for j in [0..n-1] {
for i in [0..m-1] {
C[i][j] = A[i][j] + B[i][j]
}
}


### ScalingEdit

Given an m-by-n matrix A and a scalar s, sA is the m-by-n matrix computed by multiplying each element in A by s. For example:

$10 \times \begin{bmatrix} 1 & 3 & 2 \\ 1 & 0 & 0 \\ 1 & 2 & 2 \end{bmatrix} = \begin{bmatrix} 1 \times 10 & 3 \times 10 & 2 \times 10\\ 1 \times 10 & 0 \times 10 & 0 \times 10\\ 1 \times 10 & 2 \times 10 & 2 \times 10 \end{bmatrix} = \begin{bmatrix} 10 & 30 & 20 \\ 10 & 0 & 0 \\ 10 & 20 & 20 \end{bmatrix}$

pseudocode

Matrix  A[m][n], B[m][n]
float   s
integer i,j

for j in [0..n-1] {
for i in [0..m-1] {
C[i][j] =  s * A[i][j]
}
}


### MultiplicationEdit

The product of two matrices is well-defined only if the number of columns of the first matrix is the same as the number of rows of the second matrix. If A is an m-by-p matrix and B is an p-by-n matrix, then their matrix product AB is the m-by-n matrix (m rows, n columns) given by:

$(AB)[i,j] = A[i,0] B[0,j] + A[i,1] B[1,j] + ... + A[i,p-1] B[p-1,j] \!\$

for each pair i and j. For example:

$\begin{bmatrix} 1 & 3 & 2 \\ 1 & 2 & 2 \end{bmatrix} \cdot \begin{bmatrix} 9 & 6\\ 8 & 5\\ 4 & 9 \end{bmatrix} = \begin{bmatrix} 1 \times 9 + 3 \times 8 + 2 \times 4 & 1 \times 6 + 3 \times 5 + 2 \times 9 \\ 1 \times 9 + 2 \times 8 + 2 \times 4 & 1 \times 6 + 2 \times 5 + 2 \times 9 \end{bmatrix} = \begin{bmatrix} 41 & 39 \\ 33 & 34 \end{bmatrix}$

pseudocode

Matrix  A[m][n], B[n][p], C[m][p]
integer i,j,k

for j in [0..n-1] {
for i in [0..m-1] {
c[i][j] = 0
for k in [0..p-1] [
C[i][j] =  C[i][j] + A[i][k] * B[k][j]
}
}
}


### TransposeEdit

The transpose of a matrix is another matrix, produced by turning rows into columns and vice versa. Informally, the transpose of a square matrix is obtained by reflecting at the main diagonal (that runs from the top left to bottom right of the matrix). The transpose of the matrix A is written as Atr, tA, A′, or AT.

Formally, the transpose of the m-by-n matrix A is the n-by-m matrix AT defined by AT[i, j] = A[j, i] for 1 ≤ in and 1 ≤ jm.

For example:

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

pseudocode

Matrix  A[m][n], B[n][m]
integer i,j,k

for j in [0..n-1] {
for i in [0..m-1] {
B[j][i] =  A[i][j]
}
}