Friday 10 July 2015

R: matrix operations

Element wise multiplication

If A and B are two matrices, then A * B performs element wise multiplication (It is not matrix multiplication).
> A <- matrix(1:9, nrow=3, ncol=3)
> B <- matrix(3:11, nrow=3, ncol=3)
> 
> A
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> 
> B
     [,1] [,2] [,3]
[1,]    3    6    9
[2,]    4    7   10
[3,]    5    8   11
> 
> A * B
     [,1] [,2] [,3]
[1,]    3   24   63
[2,]    8   35   80
[3,]   15   48   99

A * B multiplies A[1][1] with B[1][1], A[1][2] with B[1][2] etc.,

Matrix Multiplication

“A%*%B” is used to perform matrix multiplication.
> A%*%B
     [,1] [,2] [,3]
[1,]   54   90  126
[2,]   66  111  156
[3,]   78  132  186

Transpose of matrix
t(A) is used to get transpose of matrix A.

> t(A)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9


Inverse of Matrix

“solve(c)” is used to find inverse of matrix c.

> c <- matrix(c(4,3,3,2), nrow=2, ncol=2)
> c
     [,1] [,2]
[1,]    4    3
[2,]    3    2
> 
> solve(c)
     [,1] [,2]
[1,]   -2    3
[2,]    3   -4

If inverse is not possible, then you get following error, “Lapack routine dgesv: system is exactly singular:”


For example, it is not possible to compute inverse for matrix A.
> solve(A)
Error in solve.default(A) : 
  Lapack routine dgesv: system is exactly singular: U[3,3] = 0

Creates Identity matrix
If k is scalar, then diag(k) creates k*k identity matrix.
> diag(4)
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    1    0    0
[3,]    0    0    1    0
[4,]    0    0    0    1

Get principal diagonal elements of matrix

“diag(A)” returns principal diagonal elements of matrix A.

> A
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> 
> diag(A)
[1] 1 5 9

Combine matrices horizontally
“cbind(m1, m2,…mN)” combines matrices horizontally.
> A
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> 
> B
     [,1] [,2] [,3]
[1,]    3    6    9
[2,]    4    7   10
[3,]    5    8   11
> 
> 
> cbind(A,B)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    4    7    3    6    9
[2,]    2    5    8    4    7   10
[3,]    3    6    9    5    8   11

Combine matrices vertically
“rbind(m1, m2,…mN)” combines matrices horizontally.
> rbind(A,B)
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
[4,]    3    6    9
[5,]    4    7   10
[6,]    5    8   11

Get vector of row sums
rowSums(A) returns vector of row sums.

> rowSums(A)
[1] 12 15 18

Get vector of column sums
colSums(A) returns vector of column sums.

> colSums(A)
[1]  6 15 24

Get vector of row means
“rowMeans(A)” returns vector of row means.

> rowMeans(A)
[1] 4 5 6

Get vector of column means
“colMeans(A)” returns vector of column means.

> colMeans(A)
[1] 2 5 8





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment