Chap 3 - BSD2223
Chap 3 - BSD2223
Chap 3 - BSD2223
3.1 Matrices
3.11 Matrix Operations
3.12 Function on Matrix
3.2 Arrays
3.21 Array Operations
3.22 Function on Arrays
1 / 28
Learning Objectives
2 / 28
3.1 Matrices
• All entries in the matrix should be of the same data type and
with the same row or column lengths.
3 / 28
Creating Matrices
• Matrix syntax:
matrix(vector, nrow = r, ncol=c, byrow = FALSE)
4 / 28
Example 3.1 - Creating Matrices
5 / 28
Example 3.2 - Creating Matrices
(i) Create matrix using rbind(.) or cbind(.)
c1 <- 1:6
c2 <- 7:12
cbind(c1,c2) #column bind
rbind(c1,c2) #row bind
6 / 28
Example 3.3 - Creating Matrices
7 / 28
3.1.1 Matrix Operation
8 / 28
Matrix Operation
Function Description
A+B Addition, row m = p and column n = q
A−B Subtraction, row m = p and column n = q
k ∗A Scalar multiplication
A% ∗ %B Matrix multiplication, n = p
solve(A) Inverse of matrix A. Am×m (square matrix)
det(A) Determinant of matrix A, |A| (A square matrix)
9 / 28
Example 3.4 - Matrix Operation
A <- matrix(data = 1:16, nrow = 4, ncol = 4,
byrow = TRUE); A
B <- matrix(data = 16:31, nrow = 4, ncol = 4,
byrow = FALSE); B
A + B
A - B
A * B
A/B
# Matrix Algebra
5*A # scalar multiplication
A %*% B # matrix multiplication
t(A) # transpose a matrix
A %*% t(A)
10 / 28
Inverse Matrix
11 / 28
Example 3.5 Inverse Matrix - solve(.) function
# Create 3 different vectors.
a1 <- c(3, 2, 8)
a2 <- c(6, 3, 2)
a3 <- c(5, 2, 4)
12 / 28
Example 3.6 Inverse Matrix - inv(.) function
# create 3 by 3 matrix
x <- sample(1:50,9)
B <- matrix(x,nrow=3); B
13 / 28
3.1.2 Function on Matrix
Matrix is a two dimensional data structure, row (m) and
column(n). Let Am×n be a matrix, then each cell in the matrix is
denoted by aij for i = 1, 2, . . . , m and j = 1, 2, . . . , n.
Function Description
a[i,j] an element of row i and column j
a[i, ] all elements in row i
a[ ,j] all elements in column j
dim(A) dimension of matrix A
dimnames(A) labels for rows and columns
t(A) transpose of matrix A
diag(A) extract diagonal elements of matrix A
diag(k) k scalar, creates k by k identity matrix
diag(k,dim) k scalar, creates k value on the diagonal ma-
trix and set the matrix dimension: dim
14 / 28
Adding and Deleting Matrix
15 / 28
Example 3.7 - Subset and Delete Matrix
## Subset a matrix
m6 <- c(3, 5, -7, 18, 0, 0.5, 1, 9, 6)
dim(m6) <- c(3,3)
m6
16 / 28
Example 3.8 - Subset and Delete Matrix
## Subset a matrix
m6 <- c(3, 5, -7, 18, 0, 0.5)
dim(m6) <- c(3,2)
m6
## Modify a matrix
m6 <- c(3, 5, -7, 18, 0, 0.5)
dim(m6) <- c(3,2)
m6
m6[2,2] <- 99 #replace cell(2,2) with 99
m6
m6[m6 < 10] <- 0 #replace all elements < 10 with 0
m6
17 / 28
Example 3.9 - Add or Delete Elements of a Matrix
set.seed(1481)
x <- sample(-20:20, 9, replace=TRUE)
m1 <- matrix(x, nrow=3)
m1
m1[,-3] ##delete 3rd column
m1[-1, 3:2] ##delete 1st row and rearrange row 2&3
m1[-2, -c(1,3)] ##delete 2nd row and column 1&3
18 / 28
Statistical Functions on Matrix
19 / 28
apply(.) Function
20 / 28
Example 3.10 - Statistical Functions
set.seed(1481)
x1 <- sample(-20:20, 9, replace=TRUE); x1
m1 <- matrix(x1, nrow=3); m1
set.seed(58)
x2 <- sample(seq(-10,10)/2, 9, replace=TRUE); x2
m3 <- matrix(x2, nrow=3); m3
21 / 28
3.2 Arrays
22 / 28
Array Syntax
23 / 28
Example 3.11 - Array (Create and Naming)
24 / 28
Example 3.12 - Array (Extract Element)
25 / 28
Example 3.13 - Array (Algebraic Operation)
## Operation on Arrays
## Create 2 arrays
v3 <- 1:4
v4 <- c(1,5,11,10,6,12,-5,0,8,7,9,2)
v5 <- rep(2,8)
v6 <- seq(2,6, length=9)
ar4 <- array(c(v5,v6),dim = c(4,4,2)); ar4
m1+m2
26 / 28
Example 3.14 - Array (Create from Matrix)
## Array
## Form a matrix of 3 by 4 for 2 layers
ar1 <- array(1:24, dim= c(3,4,2))
ar1
## Form an array
## Form a matrix of 3 by 4 for 2 layers and 2 sets
ar2 <- array(rep(1:24, times=3), dim= c(3,4,2,2))
ar2
27 / 28
Example 3.15 - Array: apply(.) function
Syntax: apply(x, margin, fun)
## apply(.) Function
v5 <- rep(2,8)
v6 <- seq(2,6, length=8)
ar4 <- array(c(v5,v6),dim = c(4,4,2)); ar4
# fun = sum
apply(ar4, 1, sum) #c(1) sum through rows
apply(ar4, 2, sum) #c(2) sum through columns
# fun = mean
apply(ar4, 1, mean) #c(1) mean through rows
apply(ar4, 2, mean) #c(2) mean through columns
28 / 28