Matrices and Arrays

Reading Assignments

Why we need matrix?

Let’s discuss Vector vs Matrix vs Array

  • Dimensions

Vector is one-dim

c(1,2,3,4,5,6)
#> [1] 1 2 3 4 5 6

Matrix is two-dim

matrix(1:12, nrow = 3)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    4    7   10
#> [2,]    2    5    8   11
#> [3,]    3    6    9   12

Array is \(n\)-dim

  • The vector, matrix, and array are all atomic objects, i.e., only single data type can be stored in a vector, or a matrix, or an array.
    • Hierarchy Rule is the same as before:

      character > double > integer > logical

Example:

v1 <- c("A", "B", "C") # character
v2 <- c(1, 2, 3) # double
v3 <- c(TRUE, FALSE, FALSE) # logical
cbind(v1, v2, v3)
#>      v1  v2  v3     
#> [1,] "A" "1" "TRUE" 
#> [2,] "B" "2" "FALSE"
#> [3,] "C" "3" "FALSE"

Create a Matrix

  • cbind()

Example from the previous page

v1 <- c("A", "B", "C") # character
v2 <- c(1, 2, 3) # double
v3 <- c(TRUE, FALSE, FALSE) # logical
cbind(v1, v2, v3)
#>      v1  v2  v3     
#> [1,] "A" "1" "TRUE" 
#> [2,] "B" "2" "FALSE"
#> [3,] "C" "3" "FALSE"
  • rbind()
r1 <- c(1, 2, 3)
r2 <- c(4, 5, 6)
rbind(r1, r2)
#>    [,1] [,2] [,3]
#> r1    1    2    3
#> r2    4    5    6
  • matrix()
matrix(1:12, nrow = 3, ncol = 4, byrow=FALSE)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    4    7   10
#> [2,]    2    5    8   11
#> [3,]    3    6    9   12

Equivalently,

matrix(1:12, nrow = 3)
matrix(1:12, ncol = 4)
matrix(1:12, nrow = 3, ncol = 4)
  • What happens if byrow = TRUE
matrix(1:12, nrow = 3, byrow = TRUE) # Matrix is filled by row.
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    2    3    4
#> [2,]    5    6    7    8
#> [3,]    9   10   11   12

Working with dim()

dim() – get the dimension of the matrix

M <- matrix(1:12, nrow = 3)
dim(M)
#> [1] 3 4

Get the row- or col- dimension only

dim(M)[1] # row dimension
#> [1] 3
dim(M)[2] # col dimension
#> [1] 4

Use dim() to create a matrix

A = 1:12 # vector; one-dim
dim(A) <- c(3, 4)
A
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    4    7   10
#> [2,]    2    5    8   11
#> [3,]    3    6    9   12

Give Names to Rows and Columns

  • Method 1
M <- matrix(1:12, nrow = 3)
rownames(M) <- c("Row1", "Row2", "Row3")
colnames(M) <- c("Col1", "Col2", "Col3", "Col4")
M
#>      Col1 Col2 Col3 Col4
#> Row1    1    4    7   10
#> Row2    2    5    8   11
#> Row3    3    6    9   12
  • Method 2
matrix(1:12, nrow = 3,
            dimnames = list(c("Row1", "Row2", "Row3"), 
                            c("Col1", "Col2", "Col3", "Col4")))
#>      Col1 Col2 Col3 Col4
#> Row1    1    4    7   10
#> Row2    2    5    8   11
#> Row3    3    6    9   12

Basic Operations with Matrices

We use Example 7.1 as follows

# inputs
deposit = 1000
rate_savings = 0.02
rate_moneymkt = 0.025
rate_certificate = 0.03
years = 0:5

# future values
savings = deposit * (1 + rate_savings)^years
moneymkt = deposit * (1 + rate_moneymkt)^years
certificate = deposit * (1 + rate_certificate)^years

# matrix
mat = matrix(c(years, savings, moneymkt, certificate), nrow = 6, ncol = 4)

# row and columns names
rownames(mat) = 1:6
colnames(mat) = c("year", "savings", "moneymkt", "certificate")

mat
#>   year  savings moneymkt certificate
#> 1    0 1000.000 1000.000    1000.000
#> 2    1 1020.000 1025.000    1030.000
#> 3    2 1040.400 1050.625    1060.900
#> 4    3 1061.208 1076.891    1092.727
#> 5    4 1082.432 1103.813    1125.509
#> 6    5 1104.081 1131.408    1159.274

Selecting elements

mat[RowIndex, ColIndex]

Examples:

mat[2, 3] # select value in row 2 and col 3
#> [1] 1025
mat[1:2, 3:4] # select row 1 to 2 and col 3 to 4
#>   moneymkt certificate
#> 1     1000        1000
#> 2     1025        1030
mat[-1, -2] # select all other elements except row 1 and col 2
#>   year moneymkt certificate
#> 2    1 1025.000    1030.000
#> 3    2 1050.625    1060.900
#> 4    3 1076.891    1092.727
#> 5    4 1103.813    1125.509
#> 6    5 1131.408    1159.274

mat[RowIndex, ]

Examples:

mat[3, ] # select row 3
#>        year     savings    moneymkt certificate 
#>       2.000    1040.400    1050.625    1060.900
mat[1:3, ] # select row 1 to 3
#>   year savings moneymkt certificate
#> 1    0  1000.0 1000.000      1000.0
#> 2    1  1020.0 1025.000      1030.0
#> 3    2  1040.4 1050.625      1060.9
mat[-1, ] # select all elements except row 1
#>   year  savings moneymkt certificate
#> 2    1 1020.000 1025.000    1030.000
#> 3    2 1040.400 1050.625    1060.900
#> 4    3 1061.208 1076.891    1092.727
#> 5    4 1082.432 1103.813    1125.509
#> 6    5 1104.081 1131.408    1159.274

mat[,ColIndex]

Examples:

mat[, 2] # select col 2
#>        1        2        3        4        5        6 
#> 1000.000 1020.000 1040.400 1061.208 1082.432 1104.081
mat[, 2:4] # select col 2 to 4
#>    savings moneymkt certificate
#> 1 1000.000 1000.000    1000.000
#> 2 1020.000 1025.000    1030.000
#> 3 1040.400 1050.625    1060.900
#> 4 1061.208 1076.891    1092.727
#> 5 1082.432 1103.813    1125.509
#> 6 1104.081 1131.408    1159.274
mat[, -3] # select all elements except col 3
#>   year  savings certificate
#> 1    0 1000.000    1000.000
#> 2    1 1020.000    1030.000
#> 3    2 1040.400    1060.900
#> 4    3 1061.208    1092.727
#> 5    4 1082.432    1125.509
#> 6    5 1104.081    1159.274

Adding a new column using cbind() or row using rbind()

Examples:

# new column
vec <- c(2, 4, 6, 8, 10, 12)

# adding new column
mat <- cbind(mat, vec)
mat
#>   year  savings moneymkt certificate vec
#> 1    0 1000.000 1000.000    1000.000   2
#> 2    1 1020.000 1025.000    1030.000   4
#> 3    2 1040.400 1050.625    1060.900   6
#> 4    3 1061.208 1076.891    1092.727   8
#> 5    4 1082.432 1103.813    1125.509  10
#> 6    5 1104.081 1131.408    1159.274  12

Note that, in order to add new column to mat, you need to specify mat <- cbind(mat, vec), i.e. store the new matrix to mat.

Examples:

# new row
row_vec <- c(2.5, 1051.353, 1064.623, 1078.068, 7)
mat <- rbind(mat, row_vec)
mat
#>         year  savings moneymkt certificate vec
#> 1        0.0 1000.000 1000.000    1000.000   2
#> 2        1.0 1020.000 1025.000    1030.000   4
#> 3        2.0 1040.400 1050.625    1060.900   6
#> 4        3.0 1061.208 1076.891    1092.727   8
#> 5        4.0 1082.432 1103.813    1125.509  10
#> 6        5.0 1104.081 1131.408    1159.274  12
#> row_vec  2.5 1051.353 1064.623    1078.068   7

Deleting a column and/or row

Examples:

mat <- mat[, -5] # Remove column
mat
#>         year  savings moneymkt certificate
#> 1        0.0 1000.000 1000.000    1000.000
#> 2        1.0 1020.000 1025.000    1030.000
#> 3        2.0 1040.400 1050.625    1060.900
#> 4        3.0 1061.208 1076.891    1092.727
#> 5        4.0 1082.432 1103.813    1125.509
#> 6        5.0 1104.081 1131.408    1159.274
#> row_vec  2.5 1051.353 1064.623    1078.068
mat <- mat[-7, ] # Remove row
mat
#>   year  savings moneymkt certificate
#> 1    0 1000.000 1000.000    1000.000
#> 2    1 1020.000 1025.000    1030.000
#> 3    2 1040.400 1050.625    1060.900
#> 4    3 1061.208 1076.891    1092.727
#> 5    4 1082.432 1103.813    1125.509
#> 6    5 1104.081 1131.408    1159.274

Moving a column and/or row

Examples:

mat <- mat[, c(2:4, 1)] # moving columns
mat
#>    savings moneymkt certificate year
#> 1 1000.000 1000.000    1000.000    0
#> 2 1020.000 1025.000    1030.000    1
#> 3 1040.400 1050.625    1060.900    2
#> 4 1061.208 1076.891    1092.727    3
#> 5 1082.432 1103.813    1125.509    4
#> 6 1104.081 1131.408    1159.274    5

mat <- mat[6:1, ] # moving rows
mat
#>    savings moneymkt certificate year
#> 6 1104.081 1131.408    1159.274    5
#> 5 1082.432 1103.813    1125.509    4
#> 4 1061.208 1076.891    1092.727    3
#> 3 1040.400 1050.625    1060.900    2
#> 2 1020.000 1025.000    1030.000    1
#> 1 1000.000 1000.000    1000.000    0

Matrix Calculation

Matrix Addition

Examples:

(A = matrix(1:16, nrow = 4))
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    5    9   13
#> [2,]    2    6   10   14
#> [3,]    3    7   11   15
#> [4,]    4    8   12   16
(B = matrix(1:16, nrow = 4, byrow = T))
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    2    3    4
#> [2,]    5    6    7    8
#> [3,]    9   10   11   12
#> [4,]   13   14   15   16

C = A + B
C
#>      [,1] [,2] [,3] [,4]
#> [1,]    2    7   12   17
#> [2,]    7   12   17   22
#> [3,]   12   17   22   27
#> [4,]   17   22   27   32

Matrix Multiplication

Examples:

(D = A %*% B)
#>      [,1] [,2] [,3] [,4]
#> [1,]  276  304  332  360
#> [2,]  304  336  368  400
#> [3,]  332  368  404  440
#> [4,]  360  400  440  480

Element-wise Multiplication

Examples:

(E = A * B)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1   10   27   52
#> [2,]   10   36   70  112
#> [3,]   27   70  121  180
#> [4,]   52  112  180  256

Outper Product

Examples:

outer(A, B)
#> , , 1, 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    5    9   13
#> [2,]    2    6   10   14
#> [3,]    3    7   11   15
#> [4,]    4    8   12   16
#> 
#> , , 2, 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    5   25   45   65
#> [2,]   10   30   50   70
#> [3,]   15   35   55   75
#> [4,]   20   40   60   80
#> 
#> , , 3, 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    9   45   81  117
#> [2,]   18   54   90  126
#> [3,]   27   63   99  135
#> [4,]   36   72  108  144
#> 
#> , , 4, 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   13   65  117  169
#> [2,]   26   78  130  182
#> [3,]   39   91  143  195
#> [4,]   52  104  156  208
#> 
#> , , 1, 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    2   10   18   26
#> [2,]    4   12   20   28
#> [3,]    6   14   22   30
#> [4,]    8   16   24   32
#> 
#> , , 2, 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    6   30   54   78
#> [2,]   12   36   60   84
#> [3,]   18   42   66   90
#> [4,]   24   48   72   96
#> 
#> , , 3, 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   10   50   90  130
#> [2,]   20   60  100  140
#> [3,]   30   70  110  150
#> [4,]   40   80  120  160
#> 
#> , , 4, 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   14   70  126  182
#> [2,]   28   84  140  196
#> [3,]   42   98  154  210
#> [4,]   56  112  168  224
#> 
#> , , 1, 3
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    3   15   27   39
#> [2,]    6   18   30   42
#> [3,]    9   21   33   45
#> [4,]   12   24   36   48
#> 
#> , , 2, 3
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    7   35   63   91
#> [2,]   14   42   70   98
#> [3,]   21   49   77  105
#> [4,]   28   56   84  112
#> 
#> , , 3, 3
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   11   55   99  143
#> [2,]   22   66  110  154
#> [3,]   33   77  121  165
#> [4,]   44   88  132  176
#> 
#> , , 4, 3
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   15   75  135  195
#> [2,]   30   90  150  210
#> [3,]   45  105  165  225
#> [4,]   60  120  180  240
#> 
#> , , 1, 4
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    4   20   36   52
#> [2,]    8   24   40   56
#> [3,]   12   28   44   60
#> [4,]   16   32   48   64
#> 
#> , , 2, 4
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    8   40   72  104
#> [2,]   16   48   80  112
#> [3,]   24   56   88  120
#> [4,]   32   64   96  128
#> 
#> , , 3, 4
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   12   60  108  156
#> [2,]   24   72  120  168
#> [3,]   36   84  132  180
#> [4,]   48   96  144  192
#> 
#> , , 4, 4
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   16   80  144  208
#> [2,]   32   96  160  224
#> [3,]   48  112  176  240
#> [4,]   64  128  192  256
  • Let \(x\) and \(y\) be \(n\)-dim vectors, we can use x %*% y or corssprod(x, y) to calculate the inner product; we can use %o% or outer(x, y) to calculate outer product.

Example:

(x = 1:5)
#> [1] 1 2 3 4 5
(y = seq(2, 10, by = 2))
#> [1]  2  4  6  8 10

# inner product
x %*% y
#>      [,1]
#> [1,]  110

t(x) %*% y
#>      [,1]
#> [1,]  110

crossprod(x, y)
#>      [,1]
#> [1,]  110

# outer product

x %o% y
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    2    4    6    8   10
#> [2,]    4    8   12   16   20
#> [3,]    6   12   18   24   30
#> [4,]    8   16   24   32   40
#> [5,]   10   20   30   40   50

x %*% t(y)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    2    4    6    8   10
#> [2,]    4    8   12   16   20
#> [3,]    6   12   18   24   30
#> [4,]    8   16   24   32   40
#> [5,]   10   20   30   40   50

outer(x, y)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    2    4    6    8   10
#> [2,]    4    8   12   16   20
#> [3,]    6   12   18   24   30
#> [4,]    8   16   24   32   40
#> [5,]   10   20   30   40   50

Matrix Calculation

Matrix Addition

Examples:

(A = matrix(1:16, nrow = 4))
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    5    9   13
#> [2,]    2    6   10   14
#> [3,]    3    7   11   15
#> [4,]    4    8   12   16
(B = matrix(1:16, nrow = 4, byrow = T))
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    2    3    4
#> [2,]    5    6    7    8
#> [3,]    9   10   11   12
#> [4,]   13   14   15   16

C = A + B
C
#>      [,1] [,2] [,3] [,4]
#> [1,]    2    7   12   17
#> [2,]    7   12   17   22
#> [3,]   12   17   22   27
#> [4,]   17   22   27   32

Matrix Multiplication

Examples:

(D = A %*% B)
#>      [,1] [,2] [,3] [,4]
#> [1,]  276  304  332  360
#> [2,]  304  336  368  400
#> [3,]  332  368  404  440
#> [4,]  360  400  440  480

Element-wise Multiplication

Examples:

(E = A * B)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1   10   27   52
#> [2,]   10   36   70  112
#> [3,]   27   70  121  180
#> [4,]   52  112  180  256

Outper Product

Examples:

outer(A, B)
#> , , 1, 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    5    9   13
#> [2,]    2    6   10   14
#> [3,]    3    7   11   15
#> [4,]    4    8   12   16
#> 
#> , , 2, 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    5   25   45   65
#> [2,]   10   30   50   70
#> [3,]   15   35   55   75
#> [4,]   20   40   60   80
#> 
#> , , 3, 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    9   45   81  117
#> [2,]   18   54   90  126
#> [3,]   27   63   99  135
#> [4,]   36   72  108  144
#> 
#> , , 4, 1
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   13   65  117  169
#> [2,]   26   78  130  182
#> [3,]   39   91  143  195
#> [4,]   52  104  156  208
#> 
#> , , 1, 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    2   10   18   26
#> [2,]    4   12   20   28
#> [3,]    6   14   22   30
#> [4,]    8   16   24   32
#> 
#> , , 2, 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    6   30   54   78
#> [2,]   12   36   60   84
#> [3,]   18   42   66   90
#> [4,]   24   48   72   96
#> 
#> , , 3, 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   10   50   90  130
#> [2,]   20   60  100  140
#> [3,]   30   70  110  150
#> [4,]   40   80  120  160
#> 
#> , , 4, 2
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   14   70  126  182
#> [2,]   28   84  140  196
#> [3,]   42   98  154  210
#> [4,]   56  112  168  224
#> 
#> , , 1, 3
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    3   15   27   39
#> [2,]    6   18   30   42
#> [3,]    9   21   33   45
#> [4,]   12   24   36   48
#> 
#> , , 2, 3
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    7   35   63   91
#> [2,]   14   42   70   98
#> [3,]   21   49   77  105
#> [4,]   28   56   84  112
#> 
#> , , 3, 3
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   11   55   99  143
#> [2,]   22   66  110  154
#> [3,]   33   77  121  165
#> [4,]   44   88  132  176
#> 
#> , , 4, 3
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   15   75  135  195
#> [2,]   30   90  150  210
#> [3,]   45  105  165  225
#> [4,]   60  120  180  240
#> 
#> , , 1, 4
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    4   20   36   52
#> [2,]    8   24   40   56
#> [3,]   12   28   44   60
#> [4,]   16   32   48   64
#> 
#> , , 2, 4
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]    8   40   72  104
#> [2,]   16   48   80  112
#> [3,]   24   56   88  120
#> [4,]   32   64   96  128
#> 
#> , , 3, 4
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   12   60  108  156
#> [2,]   24   72  120  168
#> [3,]   36   84  132  180
#> [4,]   48   96  144  192
#> 
#> , , 4, 4
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]   16   80  144  208
#> [2,]   32   96  160  224
#> [3,]   48  112  176  240
#> [4,]   64  128  192  256
  • Let \(x\) and \(y\) be \(n\)-dim vectors, we can use x %*% y or corssprod(x, y) to calculate the inner product; we can use %o% or outer(x, y) to calculate outer product.

Example:

(x = 1:5)
#> [1] 1 2 3 4 5
(y = seq(2, 10, by = 2))
#> [1]  2  4  6  8 10

# inner product
x %*% y
#>      [,1]
#> [1,]  110

t(x) %*% y
#>      [,1]
#> [1,]  110

crossprod(x, y)
#>      [,1]
#> [1,]  110

# outer product

x %o% y
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    2    4    6    8   10
#> [2,]    4    8   12   16   20
#> [3,]    6   12   18   24   30
#> [4,]    8   16   24   32   40
#> [5,]   10   20   30   40   50

x %*% t(y)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    2    4    6    8   10
#> [2,]    4    8   12   16   20
#> [3,]    6   12   18   24   30
#> [4,]    8   16   24   32   40
#> [5,]   10   20   30   40   50

outer(x, y)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    2    4    6    8   10
#> [2,]    4    8   12   16   20
#> [3,]    6   12   18   24   30
#> [4,]    8   16   24   32   40
#> [5,]   10   20   30   40   50

Inverse of Matrix

Example:

M <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 10), nrow = 3, byrow = T)

solve(M)
#>            [,1]      [,2] [,3]
#> [1,] -0.6666667 -1.333333    1
#> [2,] -0.6666667  3.666667   -2
#> [3,]  1.0000000 -2.000000    1