class: center, middle, inverse, title-slide # Lecture 4 - Vectors and Matrices ### Ali Seyhun Saral
ali.saral@unibo.it
### 29 November 2021 --- ## Recap of Last Week * We've created vectors for plotting equations `x<- range(1,10)` and `y <- 3 * x ^ 2` -- * We've learn how to create x-y plots using the built-in r functions `plot(x,y)` -- * We've learned how to change plot parameters `plot(x,y, col="red", type="b")` -- * We've learned how to create histograms `hist(x,y)` -- * We've learned how to add lines on plots `abline(v = 10, col="red")` --- # Vectors `$$\vec{a} = \left[\begin{array}{@{}c@{}} -1 \\ 1 \\ \end{array} \right]$$` -- <img src="slides4_files/figure-html/unnamed-chunk-1-1.png" style="display: block; margin: auto;" /> -- * Representing a vector in R ```r a <- c(-1, 1) ``` --- ## Sum of two vectors <img src="slides4_files/figure-html/unnamed-chunk-3-1.png" style="display: block; margin: auto;" /> --- ## Sum of two vectors <img src="slides4_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> --- ## Sum of two vectors <img src="slides4_files/figure-html/unnamed-chunk-5-1.png" style="display: block; margin: auto;" /> --- ## Sum of two vectors <img src="slides4_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> * Sum of two vectors in R ```r a <- c(-1, 1) b <- c(2, -1) ``` -- ```r c <- a + b print(c) ``` ``` ## [1] 1 0 ``` --- ## Subtraction of two vectors * a - b <img src="slides4_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> --- ## Subtraction of two vectors * a - b <img src="slides4_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> --- ## Subtraction of two vectors * a - b <img src="slides4_files/figure-html/unnamed-chunk-11-1.png" style="display: block; margin: auto;" /> --- ## Subtraction of two vectors * a - b <img src="slides4_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> -- ```r a <- c(-1, 1) b <- c(2, -1) ``` -- ```r c <- a - b print(c) ``` ``` ## [1] -3 2 ``` --- ## Multiplying a Vector with a scalar $$ \vec{b} = [b_1,b_2] $$ $$ 3 \vec{b}= \[3b_1, 3b_2\]$$ -- ```r b <- c(1,2) print(3 * b) ``` ``` ## [1] 3 6 ``` --- ## Vector Multiplication * The default multiplication in R <span style="color:red"> is not vector multiplication </span> * It would multiply individual items separately. -- ```r a <- c(2,3) b <- c(1,2) a * b ``` ``` ## [1] 2 6 ``` -- * The mathematical *vector/matrix multiplication* operator is: `%*%`. ```r a %*% b ``` ``` ## [,1] ## [1,] 8 ``` --- # Matrices `\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}` -- ```r A <- matrix(c(1,2,3,4), nrow=2) print(A) ``` ``` ## [,1] [,2] ## [1,] 1 3 ## [2,] 2 4 ``` -- * The default option lines values column-by-column -- * You can add the option `byrow=TRUE` to give the values row-by-row -- ```r A <- matrix(c(1,2,3,4), nrow=2, byrow=TRUE) print(A) ``` ``` ## [,1] [,2] ## [1,] 1 2 ## [2,] 3 4 ``` --- ## Creating Special Matrices * A matrix of all 1's ```r matrix(1, nrow=2, ncol=3) ``` ``` ## [,1] [,2] [,3] ## [1,] 1 1 1 ## [2,] 1 1 1 ``` --- ## Creating Special Matrices * To create an identity matrix, use `diag(n)` ```r I <- diag(2) I ``` ``` ## [,1] [,2] ## [1,] 1 0 ## [2,] 0 1 ``` -- ```r A <- matrix(c(1,2,3,4), nrow=2, byrow=TRUE) print(A) ``` ``` ## [,1] [,2] ## [1,] 1 2 ## [2,] 3 4 ``` -- ```r print(A %*% I) ``` ``` ## [,1] [,2] ## [1,] 1 2 ## [2,] 3 4 ``` --- ## Matrix Multiplication The matrix multiplication uses `%*%` syntax. ```r C <- matrix(c(1,0,2,1), nrow=2, ncol=2, byrow = TRUE) print(C) ``` ``` ## [,1] [,2] ## [1,] 1 0 ## [2,] 2 1 ``` ```r D <- matrix(c(2,0,2,0), nrow=2, ncol=2, byrow = TRUE) print(D) ``` ``` ## [,1] [,2] ## [1,] 2 0 ## [2,] 2 0 ``` -- ```r print(C %*% D) ``` ``` ## [,1] [,2] ## [1,] 2 0 ## [2,] 6 0 ``` --- ## Matrix Multiplication - Order matters: `A %*% B` is not `B %*% A` - Componentwise multiplication is not matrix multiplication `A * B` is not `A %*% B` ```r C <- matrix(c(1,0,2,1), nrow=2, ncol=2, byrow = TRUE) D <- matrix(c(2,0,2,0), nrow=2, ncol=2, byrow = TRUE) ``` -- ```r print(C %*% D) ``` ``` ## [,1] [,2] ## [1,] 2 0 ## [2,] 6 0 ``` -- ```r print(D %*% C) ``` ``` ## [,1] [,2] ## [1,] 2 0 ## [2,] 2 0 ``` --- ## Matrix Multiplication - `solve(M)` will give you the inverse of a matrix. (If it exists) ```r C <- matrix(c(1,0,2,1), nrow=2, ncol=2, byrow = TRUE) invC <- solve(C) print(invC) ``` ``` ## [,1] [,2] ## [1,] 1 0 ## [2,] -2 1 ``` -- * Double check if the inverse is correct ```r print(C %*% invC) ``` ``` ## [,1] [,2] ## [1,] 1 0 ## [2,] 0 1 ``` --- ## Excercies 1- Create a matrix as follows and call it `M`: `\begin{pmatrix} 8 & 4 & 3\\ -2 & -1 & 0.5 \\ 3 & 1 & 2 \end{pmatrix}` 2- Multiply it with - Scalar 32 - Muliply with another 3 x 2 matrix you define your own 3- Take the inverse of matrix `M` --- ## Excercises 4- Create a 3x3 matrix of ones. Multiply `M` with it and see how the matrix changes. 5- Create an 3x3 identity matrix. Multiply `M` with it with and see whether M changes.