8 Matrices and arrays
When we have finished this chapter, we should be able to:
8.1 Packages we need
We need to load the following packages:
8.2 Definition of a matrix
In mathematics, a matrix X is a rectangular array of numbers, symbols, or expressions arranged in rows and columns. A matrix is defined by its dimensions, which specify the number of rows and columns it contains. For example:
In this case, the matrix is a
Then
Also
Example
The column vectors are:
The row vectors are:
The main diagonal consisted of the numbers 4, 1, and 1.
8.3 Creating a matrix in R
In R, every data object contains various attributes to describe the characteristics of the data it holds. For example, objects like matrices can be produced using the dim
(dimension) attribute, facilitating the performance of matrix algebra operations.
In R, adding a dimension attribute to a vector allows to reshape it into a 2-dimensional matrix. For example:
[,1] [,2] [,3] [,4]
[1,] 4 0 2 1
[2,] 3 1 4 2
[3,] 2 0 1 3
The dim()
is an inbuilt R function that either sets or returns the dimension of the matrix, array, or data frame. Here, the dim()
function sets the dimension for the X1
object.
Most often we create a matrix using the matrix()
function. In this case, we need to specify the number of rows and columns in the function.
Example 1: numeric matrix
X2 <- matrix(X1, nrow = 3, ncol = 4)
X2
[,1] [,2] [,3] [,4]
[1,] 4 0 2 1
[2,] 3 1 4 2
[3,] 2 0 1 3
The matrix is filled by columns (default column-wise), so entries can be thought of starting in the “upper left” corner and running down the columns. If we want the matrix to be filled by rows we must add the extra argument byrow = TRUE
in the matrix()
function, as follows:
X3 <- matrix(X1, nrow = 3, ncol = 4, byrow = TRUE)
X3
[,1] [,2] [,3] [,4]
[1,] 4 3 2 0
[2,] 1 0 2 4
[3,] 1 1 2 3
The type
of data, the class
and the dimension
of the X3
object are:
Of note, the typeof()
function gives the type of data that the object includes (double), while the class
is the type of structure (matrix) of the object.
In this example, the dim()
function takes the R object, X3, as an argument and returns its dimension.
Example 2: logical matrix
x_logical <- c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE)
X4 <- matrix(x_logical, nrow = 2, ncol = 3)
X4
[,1] [,2] [,3]
[1,] TRUE FALSE FALSE
[2,] FALSE TRUE FALSE
The type
of data, the class
and the dimension
of the X4
object are:
Example 3: character matrix
[,1] [,2] [,3]
[1,] "a" "c" "e"
[2,] "b" "d" "f"
The type
of data, the class
and the dimension
of the X5
object are:
8.4 Using matrix subscripts
In R, we can identify rows, columns, or elements of a matrix by using subscripts and brackets. Particularly, X[i, ] refers to the ith row of matrix X, X[ , j] refers to jth column, and X[i, j] refers to the ijth element, respectively.
The subscripts i and j can be numeric vectors in order to select multiple rows or columns, as shown in the following examples.
X <- matrix(1:10, nrow=2) # create a 2x5 numeric matrix filled by column
X
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
X[2, ] # select the 2nd row
[1] 2 4 6 8 10
X[, 2] # select the 2nd column
[1] 3 4
X[1, 4] # select the element in the 1st row, 4th column
[1] 7
X[1, c(4, 5)] # select the elements in the 1st row, 4th and 5th column
[1] 7 9
8.5 Special types of matrices
8.5.1 The square matrix
A square matrix is a matrix that has an equal number of rows and columns.
Example
In R,
[,1] [,2] [,3]
[1,] 5 1 0
[2,] 3 -1 2
[3,] 4 0 -1
The main diagonal consisted of the numbers 5, -1, and -1. In R:
diag(M)
[1] 5 -1 -1
8.5.2 The diagonal matrix
A diagonal matrix is a special type of square matrix where all the elements outside the main diagonal are zero.
Example
In R, we can create a diagonal matrix of size 3 by using the diag()
function:
8.5.3 The identity matrix
An identity matrix, often denoted as “I”, is a square matrix (i.e. the number of rows is equal to the number of columns) with ones on the main diagonal and zeros elsewhere.
Example
In R, we can create the identity matrix of size 3 by using the diag()
function:
I <- diag(3)
I
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
8.5.4 Symmetric matrix
A symmetric matrix is a square matrix that remains unchanged when we transpose it, which means we swap its rows and columns.
Example
[,1] [,2] [,3]
[1,] 13 -4 2
[2,] -4 11 -2
[3,] 2 -2 8
In S matrix, the elements at positions (1,2) and (2,1) are both -4, the elements at positions (1,3) and (3,1) are both 2, and the elements at positions (2,3) and (3,2) are both -2. This reflects the symmetry property.
8.6 Basic matrix algebra
8.6.1 The transpose of a matrix
The transpose operation simply changes columns to rows of the original matrix with dimension
Example
For a matrix A
:
the transpose matrix is:
In R:
The transpose matrix is:
t(A)
[,1] [,2]
[1,] 4 0
[2,] -1 1
[3,] -5 -2
8.6.2 Matrix addition
Matrix addition is an operation performed between two matrices of the same dimensions. The addition of matrices involves adding corresponding elements of the matrices (element-wise addition) to create a new matrix of the same dimension.
Example
Suppose we have the A and B matrices:
The addition of the two matrices gives the following new matrix:
Here, the element in the first row and first column of the new matrix is
In R:
A
[,1] [,2] [,3]
[1,] 4 -1 -5
[2,] 0 1 -2
[,1] [,2] [,3]
[1,] 3 1 -5
[2,] 0 2 -2
The addition:
A + B
[,1] [,2] [,3]
[1,] 7 0 -10
[2,] 0 3 -4
8.6.3 Scalar multiplication of matrices
In scalar multiplication, each element in the matrix is multiplied by the given number (scalar). For example:
Example
Here, the element in the first row and first column of the new matrix is
In R:
A
[,1] [,2] [,3]
[1,] 4 -1 -5
[2,] 0 1 -2
-3 * A
[,1] [,2] [,3]
[1,] -12 3 15
[2,] 0 -3 6
8.6.4 Element-wise multiplication of matrices (Hadamard product)
The element-wise multiplication of two matrices, A and B, of the same dimensions can be computed with the
Example
In this case, the element in the first row and first column of the new matrix is
In R:
A
[,1] [,2] [,3]
[1,] 4 -1 -5
[2,] 0 1 -2
B
[,1] [,2] [,3]
[1,] 3 1 -5
[2,] 0 2 -2
The output will be a matrix of the same dimensions of the original matrices:
A * B
[,1] [,2] [,3]
[1,] 12 -1 25
[2,] 0 2 4
8.6.5 Multiplication of compatible matrices (matrix product)
Suppose we have two matrices,
Example
We’ll start by demonstrating how to multiply a
In R, this can be done either with one-dimensional atomic vectors or matrices.
- Vector notation:
- Matrix notation:
[,1] [,2] [,3]
[1,] 4 -1 -5
[,1]
[1,] -5
[2,] 2
[3,] -2
# matrix multiplication
A_row1 %*% C_col1
[,1]
[1,] -12
We ended up with a matrix multiplication equivalent to the familiar dot product of vectors (see Chapter 7).
Example
Now that we are familiar with the process of multiplying a row with a column, the multiplication of larger matrices becomes straightforward. Suppose that we have the A and C matrices:
The row-by-column multiplication of the two matrices gives the following new matrix:
We observe that the produced matrix has dimension
In this case:
the element in the first row and first column of the new matrix is the result of the dot product between the first row of A and the first column of C:
the element in the first row and second column of the new matrix is the result of the dot product between the first row of A and the second column of C:
the element in the second row and first column of the new matrix is the result of the dot product between the second row of A and the first column of C:
the element in the second row and second column of the new matrix is the result of the dot product between the second row of A and the second column of C:
In R, this type of multiplication of two matrices can be performed with the dot (inner) product %*%
operator.
A
[,1] [,2] [,3]
[1,] 4 -1 -5
[2,] 0 1 -2
[,1] [,2]
[1,] -5 5
[2,] 2 1
[3,] -2 0
A %*% C
[,1] [,2]
[1,] -12 19
[2,] 6 1
Example
The matrices
Now, the
In R:
t(A)
[,1] [,2]
[1,] 4 0
[2,] -1 1
[3,] -5 -2
B
[,1] [,2] [,3]
[1,] 3 1 -5
[2,] 0 2 -2
[,1] [,2] [,3]
[1,] 12 4 -20
[2,] -3 1 3
[3,] -15 -9 29
However, it is more efficient and faster using the crossprod()
function:
crossprod(A, B)
[,1] [,2] [,3]
[1,] 12 4 -20
[2,] -3 1 3
[3,] -15 -9 29
Before inner multiplying two matrices check that the dimensions are compatible. The number of columns of the first matrix must be equal to the number of rows of the second matrix.
8.6.6 The determinant of a square matrix
The determinant of a square matrix is a scalar value that can be computed from the matrix’s elements. Let’s consider a simple 2x2 matrix:
To calculate the determinant of this matrix, we can use the formula:
In this case, we can find the determinant using expansion by minors, we can choose any row or column and calculate the determinant using smaller
Therefore:
Example
Let’s consider a 2x2 matrix:
To calculate the determinant of this matrix, we can use the formula:
In R:
Example
Therefore:
8.6.7 The inverse of a matrix
Given a square matrix E its inverse is another square matrix of the same dimensions, denoted as det(E)
is not zero, so inverse exists (the matrix is invertible).
In R, we can use the generic built-in solve()
function to find the inverse of the matrix E:
# the solve() function takes a matrix as input and returns the matrix's inverse
E_inv <- solve(E)
E_inv
[,1] [,2] [,3]
[1,] -0.25 0.75 -0.25
[2,] -0.75 0.25 0.25
[3,] 0.50 -0.50 0.50
Alternatively, we can use the inv()
function from the matlib
package for the computation of a matrix’s inverse:
inv(E)
[,1] [,2] [,3]
[1,] -0.25 0.75 -0.25
[2,] -0.75 0.25 0.25
[3,] 0.50 -0.50 0.50
Therefore, we can verify that if we multiply the matrix
E %*% E_inv
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
8.6.8 Application: calculation of the average using matrices
In ordinary algebra, the mean of a set of n
observations,
where
Let’s compute the mean using column vectors from matrix algebra.
First we define the column vectors:
and
then the mean can be computed as follows:
where
For example:
[,1]
[1,] 1
[2,] 1
[3,] 1
[4,] 1
[5,] 1
[6,] 1
[7,] 1
V <- matrix(my_values, n, 1)
V
[,1]
[1,] 2
[2,] 5
[3,] 7
[4,] -4
[5,] 8
[6,] 6
[7,] 3
8.6.9 Eigenvalues and Eigenvectors
We have already mentioned that a symmetric matrix is a square matrix that is equal to its transpose. For example:
S
[,1] [,2] [,3]
[1,] 13 -4 2
[2,] -4 11 -2
[3,] 2 -2 8
t(S)
[,1] [,2] [,3]
[1,] 13 -4 2
[2,] -4 11 -2
[3,] 2 -2 8
A symmetric matrix guarantees that its eigenvalues are real numbers. Eigenvalues and eigenvectors are highly used by the data scientists as they are the core of the data science field. For example, eigenvalues and eigenvectors are very much useful in the principal component analysis which is a dimensionality reduction technique in machine learning.
The eigen()
built-in function in R calculates the eigenvalues and eigenvectors of a symmetric matrix. It returns a named list, with eigenvalues named values and eigenvectors named vectors:
ev <- eigen(S)
ev
eigen() decomposition
$values
[1] 17 8 7
$vectors
[,1] [,2] [,3]
[1,] 0.7453560 0.6666667 0.0000000
[2,] -0.5962848 0.6666667 0.4472136
[3,] 0.2981424 -0.3333333 0.8944272
The eigenvalues are always returned in decreasing order and are 17, 8, and 7.
- The first column vector
represents the eigenvector corresponding to the eigenvalue 17. - The second column vector
corresponds to the eigenvector for the eigenvalue 8. - The third column vector
corresponds to the eigenvector for the eigenvalue 7.
8.7 Arrays
8.7.1 Creating an array
Arrays are similar to matrices but can have more than two dimensions. They’re created with an array()
function from base R:
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
, , 3
[,1] [,2] [,3]
[1,] 13 15 17
[2,] 14 16 18
, , 4
[,1] [,2] [,3]
[1,] 19 21 23
[2,] 20 22 24
As we can see, arrays are an extension of matrices. Like matrices, they contain a single type of data (e.g., numeric).
We can find the type
, class
and the dimensions
of the array:
8.7.2 Indexing in an array
To access a particular matrix of the array, for example the 3rd matrix, we type:
# access the 3rd matrix of the array
my_array[, , 3]
[,1] [,2] [,3]
[1,] 13 15 17
[2,] 14 16 18
# access the 2nd row of the 3rd matrix of the array.
my_array[2, , 3]
[1] 14 16 18
# access the element in the 1st row and 3rd column of the 3rd matrix
my_array[1, 3, 3]
[1] 17