Mathematics for AI: Matrices
A matrix is a table of values with rows and columns.
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:
To add them, you add each element in A to the matching element in B:
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:
Now multiply it by the scalar 3:
(1)(3) = 3
(2)(3) = 6
(3)(3) = 9
(4)(3) = 12
The resulting matrix is:
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:
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:
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:
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: