Workshop 3.2: Matrices

Murray Logan

15 Jul 2017

Matrices

\(y_{1}\) \(y_{2}\) \(y_{3}\) \(y_{4}\) ... \(y_{j}\) ... \(y_{p}\)

\(y_{11}\)

\(y_{12}\)

\(y_{13}\)

\(y_{14}\)

...

\(y_{1j}\)

...

\(y_{1p}\)

\(y_{21}\)

\(y_{22}\)

\(y_{23}\)

\(y_{24}\)

...

\(y_{2j}\)

...

\(y_{2p}\)

\(y_{31}\)

\(y_{32}\)

\(y_{33}\)

\(y_{34}\)

...

\(y_{3j}\)

...

\(y_{3p}\)

.

.

.

.

...

.

...

.

\(y_{i1}\)

\(y_{i2}\)

\(y_{i3}\)

\(y_{i4}\)

...

\(y_{ij}\)

...

\(y_{ip}\)

.

.

.

.

...

.

...

.

\(y_{n1}\)

\(y_{n2}\)

\(y_{n3}\)

\(y_{n4}\)

...

\(y_{nj}\)

...

\(y_{np}\)

\[\textbf{Y} = [y_{ij}] = \begin{bmatrix} y_{11} & y_{12} &. &. &. & y_{1p}\\ y_{21} & y_{22} &. &. &. & y_{2p}\\ . &. &. &. &. & .\\ . &. &. &. &. & .\\ . &. &. &. &. & .\\ y_{n1} & y_{n2} &. &. &. & y_{np}\\ \end{bmatrix} \]

Constructing matrices

> matrix(c(1,5,10,0,
+          4,2,3,6,
+          1,7,4,1),nrow=4,ncol=3)
     [,1] [,2] [,3]
[1,]    1    4    1
[2,]    5    2    7
[3,]   10    3    4
[4,]    0    6    1
> M<-matrix(c(1,5,10,
+ 0,4,2,
+ 3,6,1,
+ 7,4,1),nrow=4,ncol=3,byrow=TRUE)
> M
     [,1] [,2] [,3]
[1,]    1    5   10
[2,]    0    4    2
[3,]    3    6    1
[4,]    7    4    1

Properties

> class(M)
[1] "matrix"

Properties

Order = dimensions

> class(M)
[1] "matrix"
> #dimensions
> dim(M)
[1] 4 3
> nrow(M)
[1] 4
> ncol(M)
[1] 3

Properties

> class(M)
[1] "matrix"
> #dimensions
> dim(M)
[1] 4 3
> nrow(M)
[1] 4
> ncol(M)
[1] 3
> colnames(M) <- c('Sp1','Sp2','Sp3')
> rownames(M) <- paste('Site',1:4)
> M
       Sp1 Sp2 Sp3
Site 1   1   5  10
Site 2   0   4   2
Site 3   3   6   1
Site 4   7   4   1

Transposing matrices

If a matrix (\(\textbf{Y}\)) is: \[ \textbf{Y}=[y_{ij}]= \begin{bmatrix} 7 & 18 & -2 & 6\\ 3 & 55 & 1 & 9\\ -4 & 0 & 31 & 7\\ \end{bmatrix} \]



Then the transposed matrix (\(\textbf{Y'}\)) is: \[ \textbf{Y}=[y_{ji}]= \begin{bmatrix} 7 & 3 & -4\\ 18 & 55 & 0\\ -2 & 1 & 31\\ 6 & 9 & 7\\ \end{bmatrix} \]


> Y <- matrix(c(7,18,-2,22
+ -16,3,55,1,
+ 9,-4,0,31
+ ),nrow=3,ncol=4,byrow=TRUE)
> Y
     [,1] [,2] [,3] [,4]
[1,]    7   18   -2    6
[2,]    3   55    1    9
[3,]   -4    0   31    7
> #transpose Y
> t(Y)
     [,1] [,2] [,3]
[1,]    7    3   -4
[2,]   18   55    0
[3,]   -2    1   31
[4,]    6    9    7

Matrix algebra

Conformable matrices - same order

Matrix addition

\[ \textbf{A}= \begin{bmatrix} 1&2&3\\ 4&5&6\\ \end{bmatrix} ~,~ \textbf{B}= \begin{bmatrix} 7&8&9\\ 10&11&12\\ \end{bmatrix} \] \[ \begin{array}{rcl} \textbf{A+B} &=& \begin{bmatrix} 1+7 & 2+8 & 3+9\\ 4+10 & 5+11 & 6+12\\ \end{bmatrix}\\ &=&\begin{bmatrix} 8 & 10 & 12\\ 14 & 16 & 18\\ \end{bmatrix} \end{array} \]
> (A <- matrix(c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
> (B <- matrix(c(7,8,9,10,11,12),nrow=2,ncol=3,byrow=TRUE))
     [,1] [,2] [,3]
[1,]    7    8    9
[2,]   10   11   12
> A+B
     [,1] [,2] [,3]
[1,]    8   10   12
[2,]   14   16   18

Matrix algebra

Conformable matrices - same order

Matrix subtraction

\[ \textbf{A}= \begin{bmatrix} 1&2&3\\ 4&5&6\\ \end{bmatrix} ~,~ \textbf{B}= \begin{bmatrix} 7&8&9\\ 10&11&12\\ \end{bmatrix} \] \[ \begin{array}{rcl} \textbf{B-A} &=&\textbf{B+(-1)A}\\ &=&\begin{bmatrix} 7+(-1) & 8+(-2) & 9+(-3)\\ 10+(-4) & 11+(-5) & 12+(-6)\\ \end{bmatrix}\\ &=&\begin{bmatrix} 6 & 6 & 6\\ 6 & 6 & 6\\ \end{bmatrix} \end{array} \]
> (A <- matrix(c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
> (B <- matrix(c(7,8,9,10,11,12),nrow=2,ncol=3,byrow=TRUE))
     [,1] [,2] [,3]
[1,]    7    8    9
[2,]   10   11   12
> B-A
     [,1] [,2] [,3]
[1,]    6    6    6
[2,]    6    6    6

Matrix algebra

Conformable - nrow(A)=ncol(B)

Matrix multiplication

\[ \textbf{A}= \begin{bmatrix} 1&2&3\\ 4&5&6\\ \end{bmatrix} ~,~ \textbf{B}= \begin{bmatrix} 1&5\\ -3&2\\ 0&-1\\ \end{bmatrix} \] \[ \begin{array}{rcl} \textbf{AB} &=& \begin{bmatrix} (1\times 1)+(2\times -3)+(3\times 0)&(1\times 5)+(2\times 2)+(3\times -1)\\ (4\times 1)+(5\times -3)+(6\times 0)&(4\times 5)+(5\times 2)+(6\times -1)\\ \end{bmatrix}\\ &=&\begin{bmatrix} 5&6\\ -11&24\\ \end{bmatrix} \end{array} \]
> A <- matrix(c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE)
> B <- matrix(c(1,-3,0,5,2,-1),nrow=3,ncol=2)
> A %*% B
     [,1] [,2]
[1,]   -5    6
[2,]  -11   24

Matrix algebra

Conformable - dim(A)=dim(B)

Element wise products

\[ \textbf{A}= \begin{bmatrix} 1&2&3\\ 4&5&6\\ \end{bmatrix} ~,~ \textbf{B}= \begin{bmatrix} 7&8&9\\ 10&11&12\\ \end{bmatrix} \] \[ \begin{array}{rcl} \textbf{A}\odot\textbf{B} &=& \begin{bmatrix} 1\times 7 & 2\times 8 & 3\times 9\\ 4\times 10 & 5\times 11 & 6\times 12\\ \end{bmatrix}\\ &=&\begin{bmatrix} 7 & 16 & 27\\ 40 & 55 & 72\\ \end{bmatrix} \end{array} \]
> A <- matrix(c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE)
> B <- matrix(c(7,8,9,10,11,12),nrow=2,ncol=3, byrow=TRUE)
> A * B
     [,1] [,2] [,3]
[1,]    7   16   27
[2,]   40   55   72

Matrix algebra

Sweeping

\[ \textbf{A}= \begin{bmatrix} 1&2&3\\ 4&5&6\\ \end{bmatrix} ~,~ \textbf{B}= \begin{bmatrix} 2\\ 3\\ \end{bmatrix} \] \[ \begin{array}{rcl} \textbf{A}\times\textbf{B} &=& \begin{bmatrix} 1\times 2 & 2\times 2 & 3\times 2\\ 4\times 3 & 5\times 3 & 6\times 3\\ \end{bmatrix}\\ &=&\begin{bmatrix} 2 & 4 & 6\\ 12 & 15 & 18\\ \end{bmatrix} \end{array} \]
> A <- matrix(c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE)
> B <- matrix(c(2,3), nrow=2, ncol=1, byrow=TRUE)
> sweep(A,1,B,'*')
     [,1] [,2] [,3]
[1,]    2    4    6
[2,]   12   15   18

Matrix algebra

Sweeping

\[ \textbf{A}= \begin{bmatrix} 1&2&3\\ 4&5&6\\ \end{bmatrix} ~,~ \textbf{B}= \begin{bmatrix} 2&3&4\\ \end{bmatrix} \] \[ \begin{array}{rcl} \textbf{A}+\textbf{B} &=& \begin{bmatrix} 1 + 2 & 2+3 & 3+4\\ 4+2 & 5+3 & 6+4\\ \end{bmatrix}\\ &=&\begin{bmatrix} 3 & 6 & 7\\ 6 & 8 & 10\\ \end{bmatrix} \end{array} \]
> A <- matrix(c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE)
> B <- matrix(c(2,3,4), nrow=1, ncol=3, byrow=FALSE)
> sweep(A,2,B,'+')
     [,1] [,2] [,3]
[1,]    3    5    7
[2,]    6    8   10

Matrix modifications via matrix algebra

Multiplying rows