Workshop 3.2: Matrices Murray Logan 15 Jul 2017 ... . ... . . - - PowerPoint PPT Presentation

workshop 3 2 matrices
SMART_READER_LITE
LIVE PREVIEW

Workshop 3.2: Matrices Murray Logan 15 Jul 2017 ... . ... . . - - PowerPoint PPT Presentation

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 1 j y 1 p y 21 y 22 y 23 y


slide-1
SLIDE 1

Workshop 3.2: Matrices

Murray Logan 15 Jul 2017

slide-2
SLIDE 2

Matrices

y1 y2 y3 y4

...

yj

...

yp y11 y12 y13 y14

...

y1j

...

y1p y21 y22 y23 y24

...

y2j

...

y2p y31 y32 y33 y34

...

y3j

...

y3p

. . . . ... . ... .

yi1 yi2 yi3 yi4

...

yij

...

yip

. . . . ... . ... .

yn1 yn2 yn3 yn4

...

ynj

...

ynp Y = [yij] =

       

y11 y12

. . .

y1p y21 y22

. . .

y2p

. . . . . . . . . . . . . . . . . .

yn yn ynp

       

slide-3
SLIDE 3

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,] 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,] 4 2 [3,] 3 6 1 [4,] 7 4 1

slide-4
SLIDE 4

Properties

> class(M) [1] "matrix"

slide-5
SLIDE 5

Properties

O r d e r = d i m e n s i

  • n

s

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

slide-6
SLIDE 6

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 4 2 Site 3 3 6 1 Site 4 7 4 1

slide-7
SLIDE 7

Transposing matrices

If a matrix (Y) is: Y = [yij] =

  7 18 −2 6 3 55 1 9 −4 31 7  

Then the transposed matrix (Y฀) is: Y = [yji] =

    7 3 −4 18 55 −2 1 31 6 9 7    

> 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

31 7 > #transpose Y > t(Y) [,1] [,2] [,3] [1,] 7 3

  • 4

[2,] 18 55 [3,]

  • 2

1 31 [4,] 6 9 7

slide-8
SLIDE 8

Matrix algebra

Conformable matrices - same order

M a t r i x a d d i t i

  • n

A =

[ 1 2 3 4 5 6 ] , B = [ 7 8 9 10 11 12 ]

A+B

= [ 1 + 7 2 + 8 3 + 9 4 + 10 5 + 11 6 + 12 ] = [ 8 10 12 14 16 18 ]

> (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

slide-9
SLIDE 9

Matrix algebra

Conformable matrices - same order

M a t r i x s u b t r a c t i

  • n

A =

[ 1 2 3 4 5 6 ] , B = [ 7 8 9 10 11 12 ]

B-A

=

B+(-1)A

= [ 7 + (−1) 8 + (−2) 9 + (−3) 10 + (−4) 11 + (−5) 12 + (−6) ] = [ 6 6 6 6 6 6 ]

> (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

slide-10
SLIDE 10

Matrix algebra

Conformable - nrow(A)=ncol(B)

M a t r i x m u l t i p l i c a t i

  • n

A =

[ 1 2 3 4 5 6 ] , B =   1 5 −3 2 −1  

AB

= [ (1 × 1) + (2 × −3) + (3 × 0) (1 × 5) + (2 × 2) + (3 × (4 × 1) + (5 × −3) + (6 × 0) (4 × 5) + (5 × 2) + (6 × = [ 5 6 −11 24 ]

> 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

slide-11
SLIDE 11

Matrix algebra

Conformable - dim(A)=dim(B)

E l e m e n t w i s e p r

  • d

u c t s

A =

[ 1 2 3 4 5 6 ] , B = [ 7 8 9 10 11 12 ]

A ⊙ B

= [ 1 × 7 2 × 8 3 × 9 4 × 10 5 × 11 6 × 12 ] = [ 7 16 27 40 55 72 ]

> 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

slide-12
SLIDE 12

Matrix algebra

S w e e p i n g

A =

[ 1 2 3 4 5 6 ] , B = [ 2 3 ]

A × B

= [ 1 × 2 2 × 2 3 × 2 4 × 3 5 × 3 6 × 3 ] = [ 2 4 6 12 15 18 ]

> 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

slide-13
SLIDE 13

Matrix algebra

S w e e p i n g

A =

[ 1 2 3 4 5 6 ] , B = [2 3 4]

A + B

= [ 1 + 2 2 + 3 3 + 4 4 + 2 5 + 3 6 + 4 ] = [ 3 6 7 6 8 10 ]

> 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

slide-14
SLIDE 14

Matrix modifications via matrix algebra

M u l t i p l y i n g r

  • w

s