Objective of this document

The concept of matrix and vectors

Linear algebra is one of the simplest yet most useful concepts used in mathematics.

A matrix of dimension \((m,n)\) is a table of numbers with \(m\) rows and \(n\) columns

A row vector is a matrix of dimension \((1,n)\).

A column vector (or vector for short) is a matrix of dimension \((m,1)\).

Matrices represent linear operators between vector spaces.

You may also see a \((m,n)\)-matrix either as a set of \(m\) row vectors in \(\matbb{R}^n\) or as a set of \(n\) column-vectors in \(\mathbb{R}^m\).

Cross-product:

Dot-product:

The determinant of a matrix is the volume of the simplex built with the column-vectors of the matrix:

Vectors

how do I define a vector in R?

Let’s explore several methods

define a regular sequence

# define a vector of 32 values
v <- 1:32
v
##  [1]  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] 26 27 28 29 30 31 32
# define a vector 
v <- seq(from=-10,to=10,by=0.1)
v
##   [1] -10.0  -9.9  -9.8  -9.7  -9.6  -9.5  -9.4  -9.3  -9.2  -9.1  -9.0  -8.9
##  [13]  -8.8  -8.7  -8.6  -8.5  -8.4  -8.3  -8.2  -8.1  -8.0  -7.9  -7.8  -7.7
##  [25]  -7.6  -7.5  -7.4  -7.3  -7.2  -7.1  -7.0  -6.9  -6.8  -6.7  -6.6  -6.5
##  [37]  -6.4  -6.3  -6.2  -6.1  -6.0  -5.9  -5.8  -5.7  -5.6  -5.5  -5.4  -5.3
##  [49]  -5.2  -5.1  -5.0  -4.9  -4.8  -4.7  -4.6  -4.5  -4.4  -4.3  -4.2  -4.1
##  [61]  -4.0  -3.9  -3.8  -3.7  -3.6  -3.5  -3.4  -3.3  -3.2  -3.1  -3.0  -2.9
##  [73]  -2.8  -2.7  -2.6  -2.5  -2.4  -2.3  -2.2  -2.1  -2.0  -1.9  -1.8  -1.7
##  [85]  -1.6  -1.5  -1.4  -1.3  -1.2  -1.1  -1.0  -0.9  -0.8  -0.7  -0.6  -0.5
##  [97]  -0.4  -0.3  -0.2  -0.1   0.0   0.1   0.2   0.3   0.4   0.5   0.6   0.7
## [109]   0.8   0.9   1.0   1.1   1.2   1.3   1.4   1.5   1.6   1.7   1.8   1.9
## [121]   2.0   2.1   2.2   2.3   2.4   2.5   2.6   2.7   2.8   2.9   3.0   3.1
## [133]   3.2   3.3   3.4   3.5   3.6   3.7   3.8   3.9   4.0   4.1   4.2   4.3
## [145]   4.4   4.5   4.6   4.7   4.8   4.9   5.0   5.1   5.2   5.3   5.4   5.5
## [157]   5.6   5.7   5.8   5.9   6.0   6.1   6.2   6.3   6.4   6.5   6.6   6.7
## [169]   6.8   6.9   7.0   7.1   7.2   7.3   7.4   7.5   7.6   7.7   7.8   7.9
## [181]   8.0   8.1   8.2   8.3   8.4   8.5   8.6   8.7   8.8   8.9   9.0   9.1
## [193]   9.2   9.3   9.4   9.5   9.6   9.7   9.8   9.9  10.0

With the combine operator

# define a vector of 32 values
v <- 1:32
M <- matrix(v,ncol=2)
M
##       [,1] [,2]
##  [1,]    1   17
##  [2,]    2   18
##  [3,]    3   19
##  [4,]    4   20
##  [5,]    5   21
##  [6,]    6   22
##  [7,]    7   23
##  [8,]    8   24
##  [9,]    9   25
## [10,]   10   26
## [11,]   11   27
## [12,]   12   28
## [13,]   13   29
## [14,]   14   30
## [15,]   15   31
## [16,]   16   32

By reading values in a file with scan

In Rstudio, select File> New File>Text File

Fill the file with numbers

e.g.

1 2 5 7

9

10 20 3

Save the file under whatever name, say dummy.txt

# read the file bete.txt where I have saved the data I want to read
# you must provide either the absolute or the relative path of the file
# here I choose the relative path of the file bete.txt
# the path is relative to the place where the current R markdown file
# that I am using here is stored
v <- scan(file="../Data/bete.txt")
v
## [1]  1  2  5  7  9 10 20  3

If you think it is too complicated to provide the path of the data file, you can open a navigation menu to let you retrieve the file yourself by browsing files and changing directories/folders on your computer

# 
v <- scan(file=file.choose())
v
## [1]  1  2  5  7  9 10 20  3

You can plug the same method in the read.csv function. Try it.

Which operations can I do on vectors?

On a single vector:

v <- c(3,5,8)
# compute the euclidean norm of the vector
norm(v,type="2")
## [1] 9.899495
w <- c(1,1,1)
# returns sqrt(3) as expected
norm(w,type="2")
## [1] 1.732051
z1 <- cos(v)
z1
## [1] -0.9899925  0.2836622 -0.1455000
z1 <- v^2
z1
## [1]  9 25 64
z1 <- 2/v
z1
## [1] 0.6666667 0.4000000 0.2500000
z1 <- v%%2
z1
## [1] 1 1 0
z1 <- min(v)
z1
## [1] 3
z1 <- max(v)
z1
## [1] 8
z1 <- sum(v)
z1
## [1] 16
z1 <- prod(v)
z1
## [1] 120

Define your own operation on vectors:

norm_vec <- function(x) sqrt(sum(x^2))
w <- c(1,1,1)
# now call the function you just define with argument w and save the result to variable nw
nw <- norm_vec(w)

Play with two vectors:

v <- c(3,5,8)
w <- c(1,2,4)
# this will 
z1 <- v*w
z1
## [1]  3 10 32
z2 <- v %o% w
z2
##      [,1] [,2] [,3]
## [1,]    3    6   12
## [2,]    5   10   20
## [3,]    8   16   32
# sum of two vectors
z3 <- v+w
z3
## [1]  4  7 12
# the cross product of two vectors is related to the area of the parallelogram
# formed by the 2 vectors
z4 <- crossprod(v,w)
z4
##      [,1]
## [1,]   45

Define your own operation on two vectors:

dist_vec <- function(x,y) sqrt(sum((x-y)^2))
w1 <- c(0,0,0)
w2 <- c(1,1,1)
# now call the function you just define with argument w and save the result to variable nw
distance_between_w1_and_w2 <- dist_vec(w1,w2)
distance_between_w1_and_w2
## [1] 1.732051

How do I calculate the outer product of two vectors?

# 
v <- c(1,2,3)
w <- c(10,20,30)

v %o% w
##      [,1] [,2] [,3]
## [1,]   10   20   30
## [2,]   20   40   60
## [3,]   30   60   90

Matrices

how do I define a matrix in R?

You can easily define an empty matrix of size (m,n) in R

# 
m = matrix(, nrow = 10, ncol = 5)
print("Empty matrix of 10 rows and 5 columns:")
## [1] "Empty matrix of 10 rows and 5 columns:"
print(m)
##       [,1] [,2] [,3] [,4] [,5]
##  [1,]   NA   NA   NA   NA   NA
##  [2,]   NA   NA   NA   NA   NA
##  [3,]   NA   NA   NA   NA   NA
##  [4,]   NA   NA   NA   NA   NA
##  [5,]   NA   NA   NA   NA   NA
##  [6,]   NA   NA   NA   NA   NA
##  [7,]   NA   NA   NA   NA   NA
##  [8,]   NA   NA   NA   NA   NA
##  [9,]   NA   NA   NA   NA   NA
## [10,]   NA   NA   NA   NA   NA

You can fill it with values:

# 
m[1,1] <- 23
m[3,2] <- 32
m[5,5] <- 55
m
##       [,1] [,2] [,3] [,4] [,5]
##  [1,]   23   NA   NA   NA   NA
##  [2,]   NA   NA   NA   NA   NA
##  [3,]   NA   32   NA   NA   NA
##  [4,]   NA   NA   NA   NA   NA
##  [5,]   NA   NA   NA   NA   55
##  [6,]   NA   NA   NA   NA   NA
##  [7,]   NA   NA   NA   NA   NA
##  [8,]   NA   NA   NA   NA   NA
##  [9,]   NA   NA   NA   NA   NA
## [10,]   NA   NA   NA   NA   NA

how do I shape a vector into a matrix?

v <- 1:20
mfromv <- matrix(v,ncol=4)
mfromv
##      [,1] [,2] [,3] [,4]
## [1,]    1    6   11   16
## [2,]    2    7   12   17
## [3,]    3    8   13   18
## [4,]    4    9   14   19
## [5,]    5   10   15   20

or if you want to fill it by rows:

mfromv <- matrix(v,ncol=4,byrow=TRUE)
mfromv
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    5    6    7    8
## [3,]    9   10   11   12
## [4,]   13   14   15   16
## [5,]   17   18   19   20

You prefer two rows?

v <- 1:20
mfromv <- matrix(v,nrow=2)
mfromv
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,]    1    3    5    7    9   11   13   15   17    19
## [2,]    2    4    6    8   10   12   14   16   18    20

You want random values?

v <- runif(20)
mfromv <- matrix(v,nrow=2)
mfromv
##           [,1]       [,2]      [,3]      [,4]      [,5]       [,6]      [,7]
## [1,] 0.1383221 0.88329072 0.9607952 0.1208457 0.6252625 0.05669906 0.6716259
## [2,] 0.9748019 0.08391923 0.8650422 0.4596386 0.3572746 0.48567579 0.7864093
##           [,8]      [,9]     [,10]
## [1,] 0.8740628 0.9070251 0.2788597
## [2,] 0.3440356 0.9555580 0.1515182

Do you want a square matrix with values drawn from a standard normal distribution?

v <- runif(20)
mfromv <- matrix(v,nrow=2)
mfromv
##           [,1]      [,2]      [,3]      [,4]       [,5]       [,6]      [,7]
## [1,] 0.5767770 0.3745216 0.7881523 0.8217432 0.52837407 0.04051234 0.2739755
## [2,] 0.9914167 0.9434442 0.4605880 0.9347544 0.05992019 0.39984250 0.9871437
##            [,8]      [,9]     [,10]
## [1,] 0.07277081 0.6482849 0.1407622
## [2,] 0.31485998 0.2006544 0.1744426

You want to glue two matrices side by side?

Use column-bind

a <- matrix(rbinom(10,size=9,prob=0.2),ncol=2)
b <- matrix(rbinom(15,size=9,prob=0.2),ncol=3)
a
##      [,1] [,2]
## [1,]    3    2
## [2,]    2    1
## [3,]    2    0
## [4,]    1    1
## [5,]    3    2
b
##      [,1] [,2] [,3]
## [1,]    2    1    2
## [2,]    2    0    3
## [3,]    2    1    1
## [4,]    1    2    1
## [5,]    1    0    2
cbind(a,b)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    3    2    2    1    2
## [2,]    2    1    2    0    3
## [3,]    2    0    2    1    1
## [4,]    1    1    1    2    1
## [5,]    3    2    1    0    2

You want to glue matrix on top of each other?

Use row-bind

a <- matrix(rbinom(10,size=9,prob=0.8),ncol=5)
b <- matrix(rbinom(15,size=9,prob=0.8),ncol=5)
a
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    7    7    8    6    8
## [2,]    9    6    9    7    7
b
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    7    7    7    9    7
## [2,]    6    5    6    7    7
## [3,]    7    6    7    8    8
rbind(a,b)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    7    7    8    6    8
## [2,]    9    6    9    7    7
## [3,]    7    7    7    9    7
## [4,]    6    5    6    7    7
## [5,]    7    6    7    8    8

how do I read a matrix from a file in R?

You can scan it but then you need to know how to shape it!

That is you must provide a parameter (number of columns or number of rows)

mat <- scan(file=file.choose())
mat <- matrix(mat, ncol = 3, byrow = TRUE)
## Warning in matrix(mat, ncol = 3, byrow = TRUE): data length [8] is not a sub-
## multiple or multiple of the number of rows [3]
mat
##      [,1] [,2] [,3]
## [1,]    1    2    5
## [2,]    7    9   10
## [3,]   20    3    1

It might be better to assume that the matrix has already been written with the right shape in the file!

What if you have no idea of the size of the matrix that is in the file?

Prepare a file called A.txt that contains:

Col1 Col2 Col3

1 3 4

2 3 4

5 6 7

Then read it:

mat1 <-read.table(file.choose(),header=TRUE,sep=" ")
## Warning in read.table(file.choose(), header = TRUE, sep = " "): incomplete final
## line found by readTableHeader on 'C:\DAL-winter2020\stat2450\Data\A.txt'
mat1
##   Col1 Col2 Col3
## 1    1    3    4
## 2    2    3    4
## 3    5    6    7

You do not remember the dimension of the matrix?

No problem:

dim(mat1)
## [1] 3 3

how do I invert a matrix?

Only square matrices have an inverse

Rectangular matrices may have a quasi-inverse

If I have a square matrix, it only has an inverse if it is not singular

A square matrix (same number of rows and columns) is singular when it is flat.

That is when it has zero volume or determinant

To calculate the determinant:

det(mat)
## [1] -430

As this is not zero, the matrix mat is not-singular and its inverse matrix exist

We can calculate it like this

solve(mat)
##             [,1]        [,2]        [,3]
## [1,]  0.04883721 -0.03023256  0.05813953
## [2,] -0.44883721  0.23023256 -0.05813953
## [3,]  0.36976744 -0.08604651  0.01162791

how do I solve a linear system of equations?

b <- c(3,5,6)
  
A <- mat

# find the vector x that is the solution of A.x = b

x <- solve(A)%*%b
x
##            [,1]
## [1,]  0.3441860
## [2,] -0.5441860
## [3,]  0.7488372

You can check that this is right:

b - A%*%x
##               [,1]
## [1,]  0.000000e+00
## [2,] -8.881784e-16
## [3,] -1.776357e-15

RATE this

Can you please rate this document on a scale of 1 to 4?

1: not useful at all

2: not useful

4: useful

5: very useful

ERRORS/TYPOS

There might still be some errors or typos in this document.

If you have found some, it means that you are doing a good job.

Thanks for sending a copy of the sections of the document that contain a mistake.

thead').parent('table').addClass('table table-condensed'); } $(document).ready(function () { bootstrapStylePandocTables(); });