\(~~~~~~\)Data Structure in R: Matrix\(~~~~~~\)

Asst. Prof. Dr. Somsak Chanaim

International College of Digital Innovation, CMU

July 6, 2026

Data Stuctures

Data Stucture in R (ref: First Steps in R)

Interactive Matrix

Matrix Topics

Matrix

In R, matrices are two-dimensional, rectangular data structures.

They can only contain elements of a single data type (e.g., all numeric, all character, etc.).

How the create the matrix

Create a matrix object using the matrix() function.

\[mat.A = \begin{bmatrix} 1&2&3&4\end{bmatrix}\]

\[mat.B = \begin{bmatrix} 1\\2\\3\\4\end{bmatrix}\]

\[mat.C = \begin{bmatrix} 1&3\\2&4\end{bmatrix}\]

\[mat.D = \begin{bmatrix} 1&2\\3&4\end{bmatrix}\]

The matrix in R is not the same as the matrix in linear algebra because of the multiplication and division operations.

\[mat.C\times mat.D = \begin{bmatrix} 1&3\\2&4\end{bmatrix} \times \begin{bmatrix} 1&2\\3&4\end{bmatrix}=\begin{bmatrix} 10&14\\14&20\end{bmatrix}\]

The multiplication operation uses the %*% function

Matrix transpose

We can compute the matrix transpose using the t() function

\[mat.A^T = \begin{bmatrix} 1&2&3&4\end{bmatrix}^T=\begin{bmatrix} 1\\2\\3\\4\end{bmatrix}\]

\[mat.B^T = \begin{bmatrix} 1\\2\\3\\4\end{bmatrix}^T=\begin{bmatrix} 1&2&3&4\end{bmatrix}\]

\[mat.C^T = \begin{bmatrix} 1&3\\2&4\end{bmatrix}^T=\begin{bmatrix} 1&2\\3&4\end{bmatrix}\]

\[mat.D^T = \begin{bmatrix} 1&2\\3&4\end{bmatrix}^T=\begin{bmatrix} 1&3\\2&4\end{bmatrix}\]

Matrix determinant

We use the det() function. (For square matrix only)

\[\begin{aligned} det(mat.C) &= det\left(\begin{bmatrix} 1&3\\2&4\end{bmatrix}\right)\\&=(1\times 4)-(2\times 3) \\&= -2\end{aligned}\]

\[\begin{aligned}det(mat.D) &= det\left(\begin{bmatrix} 1&2\\3&4\end{bmatrix}\right)\\&=(1\times 4)-(3\times 2) \\&= -2 \end{aligned}\]

Matrix inverse

We use the solve() function. (For square matrix only)

\[\begin{aligned}mat.C^{-1}&=\dfrac{1}{det(mat.C)}\begin{bmatrix} 4&-3\\-2&1\end{bmatrix}\\&=\dfrac{1}{-2}\begin{bmatrix} 4&-3\\-2&1\end{bmatrix}\\&=\begin{bmatrix} -2&1.5\\1&-0.5\end{bmatrix}\end{aligned}\]

\[\begin{aligned}mat.D^{-1}&=\dfrac{1}{det(mat.D)}\begin{bmatrix} 4&-2\\-3&1\end{bmatrix}\\&=\dfrac{1}{-2}\begin{bmatrix} 4&-3\\-2&1\end{bmatrix}\\&=\begin{bmatrix} -2&1\\1.5&-0.5\end{bmatrix}\end{aligned}\]

Solve the linear equation system \(Ax=B\)

Let

\[A=\begin{bmatrix}1&3\\2&4\end{bmatrix},~x=\begin{bmatrix}x_1\\x_2\end{bmatrix},~B=\begin{bmatrix}5\\13\end{bmatrix} \]

We can solve the linear equation system with the solve() function too. \[\begin{align*} Ax&=B\\ \begin{bmatrix}1&3\\2&4\end{bmatrix}\begin{bmatrix} x_1\\x_2\end{bmatrix}&=\begin{bmatrix} 5\\13\end{bmatrix} \end{align*}\]

How to access/edit the matrix

\[mat.E = \begin{bmatrix} 1&2&3\\4&5&6\\7&8&9\end{bmatrix}\]

1. Access a specific element

To access the element at row 2, column 2 of a matrix mat.E

2. Access an entire row

To access the entire second row of mat.E

or

or

3. Access an entire column

To access the entire third column of mat.E

4. Access a submatrix

You can also create a submatrix by specifying the row and column indices. For example, to extract a 2x2 submatrix from mat.E

From the object mat.E, show the values in rows 1 and 3 only.

Don’t show row 2

From the object mat.E, show the values in rows 3 and 1 respectively.

Don’t show row 2 and col 2

or

6. Access using logical conditions

To access elements that satisfy a certain condition, for example, elements greater than 5

7. Assign values to specific elements

You can also assign new values to specific elements in the matrix. For example:

Assign the previous value to a object mat.E.

change the value inside matrix

change the mat.F at (1,1) and (2,2) to 5

or use the diag() function. (diag = diagonal)

From the object mat.F, change the values in the first row to 1 and 2 respectively.

Merging two matrices with the cbind() and rbind() functions.

\[A=\begin{bmatrix}1&3\\2&4\end{bmatrix}\]

\[B=\begin{bmatrix}5&7\\6&8\end{bmatrix}\]

cbind() function: Merge by column

\[\begin{bmatrix}A&B\end{bmatrix}=\begin{bmatrix}1&3&5&7\\2&4&6&8\end{bmatrix}\]

\[\begin{bmatrix}B&A\end{bmatrix}=\begin{bmatrix}5&7&1&3\\6&8&2&4\end{bmatrix}\]

rbind() function: Merge by row

\[\begin{bmatrix}A\\B\end{bmatrix}=\begin{bmatrix}1&3\\2&4\\5&7\\6&8\end{bmatrix}\]

\[\begin{bmatrix}B\\A\end{bmatrix}=\begin{bmatrix}5&7\\6&8\\1&3\\2&4\end{bmatrix}\]

Exercise: Matrix Part 1

Exercise 1: Create a Matrix

Create a 3 × 3 matrix with the numbers 1 to 9, filled by rows.

Target output

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

Complete the code

my_matrix <- (, nrow = , byrow = )
my_matrix

Exercise 2: Access Matrix Elements

Access the element in the second row and third column of my_matrix.

Target output

[1] 6

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)

element <- [, ]
element

Exercise 3: Matrix Dimensions

Find the number of rows and columns in my_matrix. Hint: use dim() function.

Target output

[1] 3 3

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)

dims <- ()
dims

Exercise 4: Matrix Addition

Create another 3 × 3 matrix my_matrix2 with values 9 to 1 filled by rows. Then add my_matrix and my_matrix2 together.

Target output

     [,1] [,2] [,3]
[1,]   10   10   10
[2,]   10   10   10
[3,]   10   10   10

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)
my_matrix2 <- (, nrow = 3, byrow = TRUE)

result_matrix <- +
result_matrix

Exercise 5: Matrix Multiplication

Perform matrix multiplication between my_matrix and my_matrix2.

Target output

     [,1] [,2] [,3]
[1,]   30   24   18
[2,]   84   69   54
[3,]  138  114   90

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)
my_matrix2 <- matrix(9:1, nrow = 3, byrow = TRUE)

product_matrix <-
product_matrix

Exercise 6: Transpose a Matrix

Find the transpose of my_matrix.

Target output

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)

transposed_matrix <- ()
transposed_matrix

Exercise 7: Matrix Determinant

Calculate the determinant of a 2 × 2 matrix my_matrix3 with values 4, 7, 2, and 6.

Target output

[1] 10

Complete the code

my_matrix3 <- (, nrow = 2)

det_value <- ()
det_value

Exercise 8: Inverse of a Matrix

Find the inverse of my_matrix3, if it exists.

Target output

     [,1] [,2]
[1,]  0.6 -0.2
[2,] -0.7  0.4

Complete the code

my_matrix3 <- matrix(c(4, 7, 2, 6), nrow = 2)

inverse_matrix <- ()
inverse_matrix

Exercise 9: Diagonal of a Matrix

Extract the diagonal elements of my_matrix.

Target output

[1] 1 5 9

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)

diagonal_elements <- ()
diagonal_elements

Exercise 10: Create an Identity Matrix

Create a 4 × 4 identity matrix.

Target output

     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    1    0    0
[3,]    0    0    1    0
[4,]    0    0    0    1

Complete the code

identity_matrix <- ()
identity_matrix

Exercise: Matrix part 2

The exercises 11 to 20 focusing on data manipulation with matrix objects in R:

Exercise 11: Element-wise Multiplication

Perform element-wise multiplication between my_matrix and a matrix of the same dimensions containing all 2’s.

Target output

     [,1] [,2] [,3]
[1,]    2    4    6
[2,]    8   10   12
[3,]   14   16   18

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)

result_matrix <- (, nrow = 3, ncol = 3)
result_matrix

Exercise 12: Row and Column Sums

Calculate the sum of each row and each column in my_matrix.

Target output

[1]  6 15 24
[1] 12 15 18

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)

row_sums <- ()
col_sums <- ()

row_sums
col_sums

Exercise 13: Add a Row to a Matrix

Add a new row c(10, 11, 12) to my_matrix and create my_matrix_extended.

Target output

        [,1] [,2] [,3]
           1    2    3
           4    5    6
           7    8    9
new_row   10   11   12

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)

new_row <- ()
my_matrix_extended <- (, )
my_matrix_extended

Exercise 14: Add a Column to a Matrix

Add a new column c(13, 14, 15, 16) to my_matrix_extended.

Target output

                 new_column
         1  2  3         13
         4  5  6         14
         7  8  9         15
new_row 10 11 12         16

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)
new_row <- c(10, 11, 12)
my_matrix_extended <- rbind(my_matrix, new_row)

new_column <- ()
my_matrix_extended <- (, )
my_matrix_extended

Exercise 15: Subsetting a Matrix

Extract a submatrix from my_matrix that includes the first two rows and the last two columns.

Target output

     [,1] [,2]
[1,]    2    3
[2,]    5    6

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)

sub_matrix <- [, ]
sub_matrix

Exercise 16: Replace Elements in a Matrix

Replace all elements in my_matrix that are greater than 5 with the value 0.

Target output

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    0
[3,]    0    0    0

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)

[ > ] <-
my_matrix

Exercise 17: Matrix Mean

Calculate the mean of all the elements in my_matrix.

Target output

[1] 5

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)

matrix_mean <- ()
matrix_mean

Exercise 18: Apply a Function to Rows or Columns

Use the apply() function to calculate the product of each row in my_matrix.

Target output

[1]   6 120 504

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)

row_products <- (, , )
row_products

Exercise 19: Matrix to Vector

Convert my_matrix into a vector.

Target output

[1] 1 4 7 2 5 8 3 6 9

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)

matrix_vector <- ()
matrix_vector

Exercise 20: Reshape a Matrix

Reshape my_matrix into a 1 × 9 matrix.

Target output

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    4    7    2    5    8    3    6    9

Complete the code

my_matrix <- matrix(1:9, nrow = 3, byrow = TRUE)

reshaped_matrix <- (, nrow = )
reshaped_matrix

Exercise 21: Matrix Subtraction

Subtract my_matrix2 from my_matrix.

Target output

     [,1] [,2] [,3]
[1,]   -8   -6   -4
[2,]   -2    0    2
[3,]    4    6    8

Complete the code

result_matrix <-
result_matrix

Exercise 22: Matrix Trace

Calculate the trace of my_matrix.

Target output

[1] 15

Complete the code

trace_value <- (())
trace_value

Exercise 23: Maximum Value in Matrix

Find the maximum value in my_matrix.

Target output

[1] 9

Complete the code

max_value <- ()
max_value

Exercise 24: Minimum Value in Matrix

Find the minimum value in my_matrix.

Target output

[1] 1

Complete the code

min_value <- ()
min_value

Exercise 25: Matrix Standard Deviation

Calculate the standard deviation of all elements in my_matrix.

Target output

[1] 2.738613

Complete the code

matrix_sd <- ()
matrix_sd