Mathematics and simulations in Matrices- R Programming

This is an explanation of performing mathematics and simulations in relation to Matrices in R.

Two matrices ‘A and B’ are created to work with using the following code and ‘A;B’ is implemented to print the matrices.

> A=matrix(c(2,0,1,3), ncol=2)
> B=matrix(c(5,2,4,-1), ncol=2)
> A;B
     [,1] [,2]
[1,]    2    1
[2,]    0    3
     [,1] [,2]
[1,]    5    4
[2,]    2   -1

These two matrices (A & B) can be added using the following code and the resultant is given as ‘C’. The same code can be used to add an ‘n’ number of matrices.

> C<- A+B
> C
     [,1] [,2]
[1,]    7    5
[2,]    2    2

Two matrices ‘A & B’ are subtracted using the following code and the resultant is printed as ‘D’. The same code can be implemented to subtract the ‘n’ number of matrices.

> D<- A-B
> D
     [,1] [,2]
[1,]   -3   -3
[2,]   -2    4

Function ‘diag()’ can be used to build a matrix with the values in the diagonal.
In the following, a matrix of 4*4 is obtained using the ‘diag()’ function with values ‘4,1,2,3’ and printed as ‘E’.

> E<- diag(c(4,1,2,3))
> E
     [,1] [,2] [,3] [,4]
[1,]    4    0    0    0
[2,]    0    1    0    0
[3,]    0    0    2    0
[4,]    0    0    0    3

Using the same function ‘diag()’, a matrix with five 3’s can be obtained in the diagonal and printed as ‘G’.

> G<-diag(c(3,3,3,3,3))
> G
     [,1] [,2] [,3] [,4] [,5]
[1,]    3    0    0    0    0
[2,]    0    3    0    0    0
[3,]    0    0    3    0    0
[4,]    0    0    0    3    0
[5,]    0    0    0    0    3

Matrix His generated with four 1’s in the 2nd,3rd, 4th, and 5th columns of the first row.

> H<- matrix(c(0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),nrow = 5, ncol = 5, byrow = TRUE)
> H
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    1    1    1    1
[2,]    0    0    0    0    0
[3,]    0    0    0    0    0
[4,]    0    0    0    0    0
[5,]    0    0    0    0    0

Matrix 'I' is generated with four 2’s in the 2nd,3rd, 4th, and 5th rows of the first column.

> I <- matrix(c(0,0,0,0,0,2,0,0,0,0,2,0,0,0,0,2,0,0,0,0,2,0,0,0,0), nrow = 5, ncol = 5, byrow = TRUE)
> I
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    0    0
[2,]    2    0    0    0    0
[3,]    2    0    0    0    0
[4,]    2    0    0    0    0
[5,]    2    0    0    0    0

All three matrices ‘G+H+I’ are added to obtain matrix ‘mat final’ using the following code and printed as ‘matfinal’.

> matfinal<-G+H+I
> matfinal
     [,1] [,2] [,3] [,4] [,5]
[1,]    3    1    1    1    1
[2,]    2    3    0    0    0
[3,]    2    0    3    0    0
[4,]    2    0    0    3    0
[5,]    2    0    0    0    3

In this way, addition & subtraction of matrices and matrices with digits in the diagonal can be created when these codes are implemented.



URL to git repo:https://github.com/VedaVangala/vedas-r-repo/tree/main/R6

Comments

Popular posts from this blog

Input/Output, String manipulation and 'plyr' package in R

PACKAGE "ACCURACY"

Visualization of Graphics in R