Ch3.1.1 Part 2: Matrices & Matrix Operations

Matrices

  • Matrices generalize vectors, with multiple columns.
  • Here are two \( m \times n \) matrices \( A \) and \( B \), where \( m \) is the number of rows and \( n \) is the number of columns.

\[ A= \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \\ \end{bmatrix}, ~~ B= \begin{bmatrix} b_{11} & b_{12} & \cdots & b_{1n} \\ b_{21} & b_{22} & \cdots & b_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ b_{m1} & b_{m2} & \cdots & b_{mn} \\ \end{bmatrix} \]

Example 1: Matrices in R

u <- c(2,1,4,1,0,3,-8,2,5)
(A <- matrix(u,3))
     [,1] [,2] [,3]
[1,]    2    1   -8
[2,]    1    0    2
[3,]    4    3    5
dim(A)
[1] 3 3

Example 2: Matrices in R

u <- c(2,1,4,1,0,3,-8,2)
(B <- matrix(u,2,3))
     [,1] [,2] [,3]
[1,]    2    4    0
[2,]    1    1    3
dim(B)
[1] 2 3

Example 3: Matrices in R

     [,1] [,2] [,3]
[1,]    2    1   -8
[2,]    1    0    2
[3,]    4    3    5
dim(A)
[1] 3 3
     [,1] [,2] [,3]
[1,]    2    4    0
[2,]    1    1    3
dim(B)
[1] 2 3
dim(A) == dim(B)
[1] FALSE  TRUE

Example 4: Matrices in R

     [,1] [,2]
[1,]    2    0
[2,]    1    3
[3,]    4   -8
[4,]    1    2
dim(A) = ?
     [,1] [,2]
[1,]    2    1
[2,]    1    0
[3,]    4    3
dim(B) = ?
dim(A) == dim(B) 
? ?

Example 4: Matrices in R

     [,1] [,2]
[1,]    2    0
[2,]    1    3
[3,]    4   -8
[4,]    1    2
dim(A)
[1] 4 2
     [,1] [,2]
[1,]    2    1
[2,]    1    0
[3,]    4    3
dim(B)
[1] 3 2
dim(A) == dim(B)
[1] FALSE  TRUE

Matrices in R (Page 1 of 2)

u <- c(2,1,1,1,0,3,-4,2,5)
(A <- matrix(u,3))
     [,1] [,2] [,3]
[1,]    2    1   -4
[2,]    1    0    2
[3,]    1    3    5
(A <- matrix(u,3,byrow = TRUE))
     [,1] [,2] [,3]
[1,]    2    1    1
[2,]    1    0    3
[3,]   -4    2    5

Matrices in R (Page 2 of 2)

u <- c(2,1,1,1,0,3,-4,2,5)
(A <- matrix(u,3))
     [,1] [,2] [,3]
[1,]    2    1   -4
[2,]    1    0    2
[3,]    1    3    5
(A <- matrix(u,3,byrow = FALSE))
     [,1] [,2] [,3]
[1,]    2    1   -4
[2,]    1    0    2
[3,]    1    3    5

Matrices: MATLAB & Python

  • MATLAB (Preferred?)

title

  • Python (Hassle?)

title

Matrix Addition (Dimensions must match)

\[ \begin{aligned} A + B &= \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \\ \end{bmatrix} + \begin{bmatrix} b_{11} & b_{12} & \cdots & b_{1n} \\ b_{21} & b_{22} & \cdots & b_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ b_{m1} & b_{m2} & \cdots & b_{mn} \\ \end{bmatrix} \\ \\ & = \begin{bmatrix} a_{11} + b_{11} & a_{12} + b_{12} & \cdots & a_{1n} + b_{1n} \\ a_{21}+b_{21} & a_{22}+b_{22} & \cdots & a_{2n}+b_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1}+b_{m1} & a_{m2}+b_{m2} & \cdots & a_{mn}+b_{mn} \\ \end{bmatrix} \end{aligned} \]

Example 1: Matrix Addition

\[ \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} + \begin{bmatrix} 1 & 1 & 1 \\ -1 & -1 & -1 \end{bmatrix} = \begin{bmatrix} 2 & 3 & 4 \\ 3 & 4 & 5 \end{bmatrix} \]

 u <- c(1,2,3,4,5,6)
 v <- c(1,1,1,-1,-1,-1)
 A <- matrix(u,2,byrow=TRUE)
 B <- matrix(v,2,byrow=TRUE)
 A+B
      [,1] [,2] [,3]
 [1,]    2    3    4
 [2,]    3    4    5

Example 2: Matrix Addition

\[ \begin{bmatrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \end{bmatrix} + \begin{bmatrix} 1 & 1 \\ 1 & -1 \\ -1 & -1 \end{bmatrix} = ~~ ? \]

 u <- c(1,2,3,4,5,6)
 v <- c(1,1,1,-1,-1,-1)
 A <- matrix(u,3,byrow=TRUE)
 B <- matrix(v,3,byrow=TRUE)
 A + B 

Example 2: Matrix Addition

\[ \begin{bmatrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \end{bmatrix} + \begin{bmatrix} 1 & 1 \\ 1 & -1 \\ -1 & -1 \end{bmatrix} = \begin{bmatrix} 2 & 3 \\ 4 & 3 \\ 4 & 5 \end{bmatrix} \]

 u <- c(1,2,3,4,5,6)
 v <- c(1,1,1,-1,-1,-1)
 A <- matrix(u,3,byrow=TRUE)
 B <- matrix(v,3,byrow=TRUE)
 A + B
      [,1] [,2]
 [1,]    2    3
 [2,]    4    3
 [3,]    4    5

Example 1: Matrix-Scalar Addition

  • Math result

\[ \begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix} + 7 = \mathrm{Error} \]

  • R result

    u <- c(1,2,3,4,5,6)
    A <- matrix(u,3)
    A + 7
    
      [,1] [,2]
    [1,]    8   11
    [2,]    9   12
    [3,]   10   13
    

Example 2: Matrix-Vector Addition

  • Math result

\[ \begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix} + \begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix} = \mathrm{Error} \]

  • R result

    u <- c(1,2,3,4,5,6)
    A <- matrix(u,3)
    v <- c(1,1,1)
    A + v = ?
    
    Your Answer Here!
    

Example 2: Matrix-Vector Addition

  • Math result

\[ \begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix} + \begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix} = \mathrm{Error} \]

  • R result

    u <- c(1,2,3,4,5,6)
    A <- matrix(u,3)
    v <- c(1,1,1)
    A + v
    
      [,1] [,2]
    [1,]    2    5
    [2,]    3    6
    [3,]    4    7
    

Example 1: Matrix + Vector & Recycling

  • Math result

\[ \begin{bmatrix} 1 & 1 \\ 1 & 1 \\ 1 & 1 \end{bmatrix} + \begin{bmatrix} 1 \\ 2 \end{bmatrix} = \mathrm{Error} \]

  • R result

    u <- c(1,1,1,1,1,1)
    A <- matrix(u,3)
    v <- c(1,2)
    A + v
    
      [,1] [,2]
    [1,]    2    3
    [2,]    3    2
    [3,]    2    3
    

Example 2: Matrix + Vector & Recycling

  • What is the result of this addition in R?

\[ \begin{bmatrix} 1 & 1 \\ 1 & 1 \\ 1 & 1 \end{bmatrix} + \begin{bmatrix} 0 \\ 1 \end{bmatrix} = \mathrm{Error} \]

  • R result

    u <- c(1,1,1,1,1,1)
    A <- matrix(u,3)
    v <- c(0,1)
    A + v = ?
    
    Your Answer Here!
    

Example 2: Matrix + Vector & Recycling

  • What is the result of this addition in R?

\[ \begin{bmatrix} 1 & 1 \\ 1 & 1 \\ 1 & 1 \end{bmatrix} + \begin{bmatrix} 0 \\ 1 \end{bmatrix} = \mathrm{Error} \]

  • R result

    u <- c(1,1,1,1,1,1)
    A <- matrix(u,3)
    v <- c(0,1)
    A + v
    
      [,1] [,2]
    [1,]    1    2
    [2,]    2    1
    [3,]    1    2
    

Example 3: Matrix + Vector & Recycling

  • Math result

\[ \begin{bmatrix} 1 & 1 \\ 1 & 1 \\ 1 & 1 \end{bmatrix} + \begin{bmatrix} 1 \\ 2 \\ 3 \\ 4 \end{bmatrix} = \mathrm{Error} \]

  • R result

    u <- c(1,1,1,1,1,1)
    A <- matrix(u,3)
    v <- c(1,2,3,4)
    A + v
    
      [,1] [,2]
    [1,]    2    5
    [2,]    3    2
    [3,]    4    3
    

Example 4: Matrix + Vector & Recycling

  • What is the result of this addition in R?

\[ \begin{bmatrix} 2 & -1 \\ 3 & 0 \\ 1 & 4 \end{bmatrix} + \begin{bmatrix} 1 \\ 1 \\ 2 \\ 2 \end{bmatrix} = \mathrm{Error} \]

  • R result

    u <- c(2,-1,3,0,1,4)
    A <- matrix(u,3)
    v <- c(1,1,2, 2)
    A + v = ?
    
    Your Answer Here!
    

Example 4: Matrix + Vector & Recycling

  • What is the result of this addition in R?

\[ \begin{bmatrix} 2 & -1 \\ 3 & 0 \\ 1 & 4 \end{bmatrix} + \begin{bmatrix} 1 \\ 1 \\ 2 \\ 2 \end{bmatrix} = \mathrm{Error} \]

  • R result

    u <- c(2,-1,3,0,1,4)
    A <- matrix(u,3)
    v <- c(1,1,2, 2)
    A + v
    
      [,1] [,2]
    [1,]    3    2
    [2,]    0    2
    [3,]    5    5
    

What's Next

  • After this, we look at multiplication.
  • Vector-vector: u*v
  • Matrix-vector: Ax = b
  • Standard: u*v
  • Pointwise: u .* v

\[ A \mathbf{x} = \begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} = \begin{bmatrix} b_1 \\ b_2 \end{bmatrix} = \mathbf{b} \]