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 24 y 2 j y 2 p y 31 y 32 y 33 y 34 y 3 j y 3 p y i 1 y i 2 y i 3 y i 4 y ij y ip y n 1 y n 2 y n 3 y n 4 y nj y np y 11 y 12 y 1 p . . . y 21 y 22 y 2 p . . . Y = [ y ij ] = . . . . . . . . . . . . . . . . . . y n y n y np
1 0 > M [,1] [,2] [,3] [1,] 1 5 10 [2,] 4 > matrix ( c (1,5,10,0, 2 [3,] 3 6 1 [4,] 7 4 > M<- matrix ( c (1,5,10, 1 6 [2,] + 4,2,3,6, + 1,7,4,1),nrow=4,ncol=3) [,1] [,2] [,3] [1,] 1 4 1 5 0 2 7 [3,] 10 3 4 [4,] Constructing matrices + 0,4,2, + 3,6,1, + 7,4,1),nrow=4,ncol=3,byrow=TRUE)
[1] "matrix" > class (M) Properties
[1] 3 > class (M) > ncol (M) [1] 4 > nrow (M) [1] 4 3 > dim (M) > #dimensions [1] "matrix" Properties s i o n e n s d i m = d e r O r
1 5 4 7 Site 4 1 6 3 Site 3 2 4 0 Site 2 10 1 > class (M) Site 1 Sp1 Sp2 Sp3 > M > colnames (M) <- c ('Sp1','Sp2','Sp3') [1] 3 > ncol (M) [1] 4 > nrow (M) [1] 4 3 > dim (M) > #dimensions [1] "matrix" Properties > rownames (M) <- paste ('Site',1:4)
7 [2,] 0 -4 [3,] 9 1 55 3 6 7 -2 18 7 [1,] [,1] [,2] [,3] [,4] > Y + ),nrow=3,ncol=4,byrow=TRUE) 31 > #transpose Y > Y <- matrix ( c (7,18,-2,22 0 9 6 [4,] 31 1 -2 [3,] 55 > t (Y) 18 [2,] -4 3 7 [1,] [,1] [,2] [,3] + -16,3,55,1, Transposing matrices If a matrix (Y) is: 7 18 − 2 6 Y = [ y ij ] = 3 55 1 9 − 4 0 31 7 Then the transposed matrix (Y) is: 7 3 − 4 18 55 0 Y = [ y ji ] = − 2 1 31 6 9 7 + 9,-4,0,31
18 8 [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 9 > (A <- matrix ( c (1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE)) [2,] 10 11 12 > A+B [,1] [,2] [,3] [1,] 8 10 12 [2,] 14 16 [,1] [,2] [,3] Matrix algebra Conformable matrices - same order o n i t i a d d i x a t r M [ ] [ ] 1 2 3 7 8 9 A = , B = 4 5 6 10 11 12 [ ] 1 + 7 2 + 8 3 + 9 = A+B 4 + 10 5 + 11 6 + 12 [ ] 8 10 12 = 14 16 18
6 8 [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 9 > (A <- matrix ( c (1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE)) [2,] 10 11 12 > B-A [,1] [,2] [,3] [1,] 6 6 6 [2,] 6 6 [,1] [,2] [,3] Matrix algebra Conformable matrices - same order o n c t i t r a s u b i x a t r M [ ] [ ] 1 2 3 7 8 9 A = , B = 4 5 6 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
24 > A <- matrix ( c (1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE) -11 [2,] 6 -5 [1,] [,1] [,2] > A %*% B Matrix algebra Conformable - nrow(A)=ncol(B) n t i o i c a i p l u l t x m r i M a t 1 5 [ ] 1 2 3 A = , B = − 3 2 4 5 6 0 − 1 [ (1 × 1) + (2 × − 3) + (3 × 0) (1 × 5) + (2 × 2) + (3 × AB = (4 × 1) + (5 × − 3) + (6 × 0) (4 × 5) + (5 × 2) + (6 × [ ] 5 6 = − 11 24 > B <- matrix ( c (1,-3,0,5,2,-1),nrow=3,ncol=2)
72 > A <- matrix ( c (1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE) 55 40 [2,] 27 16 7 [1,] [,1] [,2] [,3] > A * B Matrix algebra Conformable - dim(A)=dim(B) s u c t r o d e p w i s t m e n E l e [ ] [ ] 1 2 3 7 8 9 A = , B = 4 5 6 10 11 12 [ ] 1 × 7 2 × 8 3 × 9 = A ⊙ B 4 × 10 5 × 11 6 × 12 [ ] 7 16 27 = 40 55 72 > B <- matrix ( c (7,8,9,10,11,12),nrow=2,ncol=3, byrow=TRUE)
18 > A <- matrix ( c (1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE) 15 12 [2,] 6 4 2 [1,] [,1] [,2] [,3] Matrix algebra g p i n w e e S [ ] [ ] 1 2 3 2 A = , B = 4 5 6 3 [ ] 1 × 2 2 × 2 3 × 2 = A × B 4 × 3 5 × 3 6 × 3 [ ] 2 4 6 = 12 15 18 > B <- matrix ( c (2,3), nrow=2, ncol=1, byrow=TRUE) > sweep (A,1,B,'*')
10 > A <- matrix ( c (1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE) 8 6 [2,] 7 5 3 [1,] [,1] [,2] [,3] Matrix algebra g p i n w e e S [ ] 1 2 3 [ 2 4 ] 3 A = , B = 4 5 6 [ ] 1 + 2 2 + 3 3 + 4 A + B = 4 + 2 5 + 3 6 + 4 [ ] 3 6 7 = 6 8 10 > B <- matrix ( c (2,3,4), nrow=1, ncol=3, byrow=FALSE) > sweep (A,2,B,'+')
Matrix modifications via matrix algebra o w s g r y i n i p l u l t M
Recommend
More recommend