Thursday, January 18, 2018

How to Perform basic Arithmetic and Linear Algebra Operations on Matrices in R Programming

Performing Linear Algebra Operations on Matrices
The common operations performed with matrices includes linear algebra operations, matrix indexing, and matrix filtering.
You can perform various linear algebra operations on matrices, such as matrix multiplication , matrix scalar multiplication, and matrix addition.
Example :
Suppose we have two matrices, x (2 by 2) and y(3 by 2) as follows
> x <- matrix(c(1,2,3,4),nrow=2,ncol=2)
> x
       [,1]  [,2]
[1,]    1    3
[2,]    2    4

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

#Combining vectors to create a matrix
Vectors can be combined by columns (cbind) and by rows (rbind). rbind will combine the vectors as rows and cbind will combine the vectors as columns, shown below example

> a <- c(4,1,3)
> b <- c(-1,3,6)
> c <- c(0,1,-1)

> m<- rbind(a,b,c)
> m
     [,1] [,2] [,3]
a    4    1    3
b   -1    3    6
c    0    1   -1

> n<-cbind(a,b,c)
> n
      a  b  c
[1,] 4 -1  0
[2,] 1  3  1
[3,] 3  6 -1

#Creating a Matrix with replicate function rep(n,times)
> m<- matrix(rep(2,9),nrow=3)
> m
      [,1] [,2] [,3]
[1,]   2    2    2
[2,]   2    2    2
[3,]   2    2    2

# Calculating Row Sums, Col Sums and the whole Matrix Sum on Matrix y

> y
        [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

> rowSums(y)
[1] 5 7 9

> colSums(y)
[1]  6 15

> sum(y)
[1] 21

#Transposing the matrix y
> y
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

> t(y)
        [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

# Arithmetical operations on matrix y by a scalar :
> y
       [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

> y+2
       [,1] [,2]
[1,]    3    6
[2,]    4    7
[3,]    5    8

> y-2
       [,1] [,2]
[1,]   -1    2
[2,]    0    3
[3,]    1    4

> y*2
        [,1] [,2]
[1,]    2   8
[2,]    4   10
[3,]    6   12

> y/2
     [  ,1] [,2]
[1,]  0.5  2.0
[2,]  1.0  2.5

[3,]  1.5  3.0

#Mathematical/Algebra operations (element wise) between matrices :
Please note that ,If two are matrices having the same dimensions(no.of rows and columns), then only we can perform the arithmetical operations between them, which will perform a component wise operation.

If two are matrices having the different dimensions(no.of rows and columns) then we will get an error saying "non-conformable arrays"
Example:
Suppose we have two matrices x ( 2 by 2 matrix ) and y (3 by 2 matrix) .

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

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

Here in above both the matrices have different dimensions, so if we try to perform any arithmetical operations between them, it will throw the error shown below.
> x+y
Error in x + y : non-conformable arrays
> y+x
Error in y + x : non-conformable arrays
> x-y
Error in x - y : non-conformable arrays
> y-x
Error in y - x : non-conformable arrays
> x*y
Error in x * y : non-conformable arrays
> y*x
Error in y * x : non-conformable arrays
> x/y
Error in x/y : non-conformable arrays
> y/x
Error in y/x : non-conformable arrays
> x+t(y)
Error in x + t(y) : non-conformable arrays
> y+t(x)
Error in y + t(x) : non-conformable arrays
> x*t(y)
Error in x * t(y) : non-conformable arrays
> y*t(y)
Error in y * t(y) : non-conformable arrays

Now we will perform the same operations on below two matrices which have same no.of dimensions ( 3 by 2 matrices). 
> y
       [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

> z
       [,1] [,2]
[1,]    4    7
[2,]    5    8
[3,]    6    9

> y+z
       [,1] [,2]
[1,]    5   11
[2,]    7   13
[3,]    9   15

> y-z
       [,1] [,2]
[1,]   -3   -3
[2,]   -3   -3
[3,]   -3   -3

> y*z
        [,1] [,2]
[1,]    4   28
[2,]   10   40
[3,]   18   54

> y/z
     [,1]      [,2]
[1,] 0.25   0.57
[2,] 0.40   0.62

[3,] 0.50    0.66

Note :
Please note that in above, the arithmetical operations are performed by the element wise 
( eg : y+z = y11+z11 , y12+z12 , ... y32+z32)

# Matrix Multiplication :
To Multiply the Matrices y and x , we will use the operator %*% as y%*%x. 
Please note to perform the multiplication between the matrices y and , the no.of columns of y should be equal to the no.of rows of x

If y has m by n (Eg : 3 by 2) Dimensions then x should have n by p (Eg: 2 by 4) dimensions then we can multiply them and the result Matrix will have m by p Dimensions ( Eg : 3 by 4 )

Example 1:
Here we have two matrices y ( 3 by 2)  and x (2 by 2)
> y <- matrix(c(1,2,3,4,5,6),nrow=3,ncol=2)
> y
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

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

> z<-y%*%x
        [,1]   [,2]
[1,]    9     19
[2,]   12    26
[3,]   15    33

How it is Calculated :
The  each element of the first row(of first matrix, y, declared at left side of operator) will be multiply to the corresponding element of the first column (of first matrix, y, declared at right side of operator), as explained below...
z11 = (y11*x11) + (y12*x21)
i.e, 9=(1*1)+(4*2)

z12 = (y11*x21) + (y12*x22)
i.e, 19=(1*3)+(4*4)

............... like wise.................

z32 = (y31*x12) + (y32*x22)
i.e, 33=(3*3)+(6*4)

For more details on this calculation, please refer the below link :
https://en.wikipedia.org/wiki/Matrix_multiplication

Example 2:
Here we have two matrices y ( 3 by 2)  and x (by 2)
> y <- matrix(c(1,2,3,4,5,6),nrow=3,ncol=2)
> y
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

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

> x%*%y
Error in x %*% y : non-conformable arguments
Here, it failed to multiply because the no.of column dimensions in x (2 rows; 2 cols) is not matching with the no.of row dimensions in y (3 rows ; 2 cols).

To fix this , just transpose the y , so that it will transpose to a matrix with 2 rows and 3 cols. Now we can try the operation again,which will get success.
> x%*%t(y)
        [,1] [,2] [,3]
[1,]   13   17   21
[2,]   18   24   30


--------------------------------------------------------------------------------------------------------
Thanks, TAMATAM ; Business Intelligence & Analytics Professional
--------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.