jueves, 23 de mayo de 2019

Summary of Matrix Operators in R

Summary of Matrix Operators in R


> 
> #To create a matrix
> A<-matrix(c(3,1,2,6,4,5),nrow=2,ncol=3,byrow=F)
> B<-matrix(c(2,1,3,6,7,4),nrow=2,ncol=3,byrow=T)
> A
     [,1] [,2] [,3]
[1,]    3    2    4
[2,]    1    6    5
> #To access an individual element in a matrix A
> A[1,2]
[1] 2
> A[1,]
[1] 3 2 4
> A[,3]
[1] 4 5
> B
     [,1] [,2] [,3]
[1,]    2    1    3
[2,]    6    7    4
> B[2,3]
[1] 4
> B[2,]
[1] 6 7 4
> B[,1]
[1] 2 6
> #Addition
> A+B
     [,1] [,2] [,3]
[1,]    5    3    7
[2,]    7   13    9
> #Subtraction
> A-B
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]   -5   -1    1
> #Multiplication with scalar
> 2*B
     [,1] [,2] [,3]
[1,]    4    2    6
[2,]   12   14    8
> B*3
     [,1] [,2] [,3]
[1,]    6    3    9
[2,]   18   21   12
> #element-by-element multiplication
> A*B
     [,1] [,2] [,3]
[1,]    6    2   12
[2,]    6   42   20
> #Transpose A'
> t(A)
     [,1] [,2]
[1,]    3    1
[2,]    2    6
[3,]    4    5
> t(B)
     [,1] [,2]
[1,]    2    6
[2,]    1    7
[3,]    3    4

Reference
Fieller, N (2016) Basics of matrix algebra for statistics with R. CRC Press

No hay comentarios:

Publicar un comentario