Lecture 3: Matrices and Arrays
August 6, 2026
๐ Last lecture we started exploring data in R and its inbuilt functionality, specifically:
๐ This lecture we look into higher-dimensional data structures such as:
\[ A:=\begin{bmatrix} 1 & 2 & 3 & 4 & 5 & 6 \\ 7 & 8 & 9 & 10 & 11 & 12 \\ 13 & 14 & 15 & 16 & 17 & 18 \\ 19 & 20 & 21 & 22 & 23 & 24 \\ 25 & 26 & 27 & 28 & 29 & 30 \end{bmatrix} \]
Note this is different from the number of dimensions of the object, which is 1 for vectors and 2 for matrices.
matrix() FunctionWe can define a matrix using the matrix() function:
The function arguments are as follows:
data โ vector of the input data (defaults to NA)nrow and ncol โ scalars specifying number of rows and columnsbyrow โ Boolean specifying row-wise or column-wise constructiondimnames โ list containing two vectors of row and column namesmatrix() FunctionWe will construct a variety of matrices using the input data below:
Firstly letโs define a 3 by 3 matrix where the data is loaded row-wise:
Letโs look more carefully at the two matrices from the last slide:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
\[ A = \begin{bmatrix}a & b & c \\ d & e & f \\ g & h & i\end{bmatrix}\implies A^T=\begin{bmatrix}a & d & g \\ b & e & h \\ c & f & i\end{bmatrix}. \]
t():If we wish to add row and column names we can use the dimnames argument:
cbind() & rbind() FunctionsAnother way of defining matrices is by binding several vectors of the same length together.
There are two ways to combine vectors:
rbind()cbind()To demonstrate we define the following vectors:
rbind() or cbind(): [,1] [,2] [,3]
v1 "alpha" "beta" "gamma"
v2 "delta" "epsilon" "kappa"
v3 "phi" "psi" "nabla"
v1 v2 v3
[1,] "alpha" "delta" "phi"
[2,] "beta" "epsilon" "psi"
[3,] "gamma" "kappa" "nabla"
๐ Notice that the rows are named with rbind() and the columns are named with cbind().
In mathematics, a multiplicative identity is a number which, when multiplied by another number, leaves the other number unchanged.
1.We can generate the identity matrix using the diag() function:
vec[3] returns the 3rd element of vec.Every matrix entry is indexed by an ordered pair \((i,j)\) where:
\[ A = \begin{bmatrix}a & b & c \\ d & e & f \\ g & h & i\end{bmatrix}\quad\text{has index}\quad\underbrace{\begin{bmatrix} (1,1) & (1,2) & (1,3) \\ (2,1) & (2,2) & (2,3) \\ (3,1) & (3,2) & (3,3) \end{bmatrix}}_{index~matrix}. \]
Consider matrix_1 from previous examples:
Indexing a whole row or column is known as slicing the matrix.
To slice a matrix we leave the first / second index values blank to return the entire column / row.
Python, which uses : to slice.: operator.Omitting values โ or even whole rows and columns โ of a matrix works the same way as omitting elements of a vector, using negative indices.
[,1] [,2] [,3]
[1,] 4 5 6
[2,] 7 8 9
[,1] [,2]
[1,] 1 3
[2,] 4 6
[3,] 7 9
03:00
Spend 3 minutes to complete the following steps:
num_matrix with elements 1 through 20 (constructed row-wise).row3.new_matrix.new_matrix using the dim() function.R has many functions designed to interface with matrices:
rowSums() and colSums()rowMeans() and colMeans()dim() and dimnames()det() โ matrix determinantsolve() โ matrix inverse%*% โ matrix multiplicationWe can also perform matrix multiplication using the %*% operator:

\[ n_1 \times n_2 \times ... \times n_k. \]
In PSTAT 10 we only consider up to 3-dimensional arrays.
We can create an array using the array() function, which has similar arguments to the matrix() function:
The arguments of the array() function are:
data โ the input data (scalar, vector, matrix)dim โ array dimensions (vector)dimnames โ array dimension names (list)We can define a \(2\times 2 \times 2\) array using the following code:
Indexing extends naturally from matrices: instead of \((i,j)\) we now index with \((i,j,k)\), one subscript per dimension:
We can apply operations (mathematical operations, functions) over an array using the apply() function:
X โ the array object on which we are applying our operationMARGIN โ vector giving the subscripts for which the function will be applied overFUN โ function to be applied03:00
For the next 3 minutes create a basic array to represent the faces of a solved two-color Rubikโs cube (each color appears on 3 of the cubeโs six faces).
red and blue as \(3 \times 3\) matrices where all entries are the character "r" and "b" respectively.red and blue matrices that represents the faces of the solved cube.Hint: one of your array dimensions should refer to the cubeโs 6 faces.
Stack 3 copies of red and 3 copies of blue along a third dimension representing the cubeโs 6 faces:

Python.$ operator.To construct an example list we first define three objects to store: (1) a character vector; (2) a numeric matrix; and (3) a logical scalar.
The key difference between indexing lists and the other data types we have considered is that to index list elements we use double braces list_example[[...]].
๐ค Today we studied lots and lots of topics:
๐คฉ Next class we continue our introduction by exploring multi-dimensional datatypes including: