Mathematics for AI: Matrices

Mathematics for AI: Matrices
Photo by ANIRUDH / Unsplash
In this post, we'll focus on building mathematical intuition around some of the most essential matrix operations used in AI.

A matrix is a table of values with rows and columns.

A = [ 1 2 3 4 5 6 ]

Matrix A has 2 rows and 3 columns (i.e. this is a 2 x 3 matrix).

Let’s take a closer look at the key matrix operations and see how they work in practice.

Matrix Addition and Subtraction

To add two matrices, they must be the same size — that means the same number of rows and columns. You add them by adding corresponding elements.

Consider two matrices A and B:

A = [ 1 2 3 4 ] B = [ 5 6 7 8 ]

To add them, you add each element in A to the matching element in B:

A + B = [ 1+5 2+6 3+7 4+8 ] = [ 6 8 10 12 ]

Matrix subtraction follows the same process as addition, but you subtract corresponding elements instead.

Matrix Multiplication

There are several ways to multiply matrices. We’ll focus on the most common types used in AI:

  • Scalar multiplication
  • Matrix multiplication

Scalar Multiplication

Scalar multiplication means multiplying every element in a matrix by a scalar to produce a new matrix. For example, take the matrix A:

A = [ 1 2 3 4 ]

Now multiply it by the scalar 3:

(1)(3) = 3
(2)(3) = 6
(3)(3) = 9
(4)(3) = 12

The resulting matrix is:

3A = [ 3 6 9 12 ]

Matrix Multiplication

Matrix multiplication involves multiplying the rows of the first matrix by the columns of the second matrix. The number of columns in the first matrix must equal the number of rows in the second matrix for matrix multiplication to be valid. Consider two matrices A and B:

A = [ 1 2 3 4 5 6 ] B = [ 7 8 9 10 11 12 ]

Matrix A has 3 columns (2 x 3) and Matrix B has 3 rows (3 x 2). Therefore these matrices can be multiplied. The resulting matrix will be 2 x 2. Consider two matrices C and D:

C = [ 1 2 3 4 5 6 ] D = [ 7 8 9 10 ]

Matrix C has 3 columns (2 x 3) and Matrix D has 2 rows (2 x 2). Therefore these matrices cannot be multiplied. The number of columns in C does not match the number of rows in D.

Worked Example

Consider two matrices A and B:

A = [ 1 2 3 4 5 6 ] B = [ 7 8 9 10 11 12 ]

The number of columns in matrix A matches the number of rows in matrix B therefore these matrices can be multiplied.

To multiply A with B, we calculate the dot product of each row in A with each column in B. The result will be a 2 × 2 matrix.

First row of A × first column of B:

(1)(7) + (2)(9) + (3)(11) = 7 + 18 + 33 = 58

First row of A × second column of B:

(1)(8) + (2)(10) + (3)(12) = 8 + 20 + 36 = 64

Second row of A × first column of B:

(4)(7) + (5)(9) + (6)(11) = 28 + 45 + 66 = 139

Second row of A × second column of B:

(4)(8) + (5)(10) + (6)(12) = 32 + 50 + 72 = 154

Final result:

AB = [ 58 64 139 154 ]