R Matrix

R Matrix is similar to vector but additionally it contains the dimension attribute. In Matrices, elements are arranged in two-dimensional rectangular layout. We use matrices containing numeric elements to be used in mathematical calculations. To create R Matrix, we use matrix() function.

R Matrix Syntax:

matrix(data,nrow,ncol,byrow,dimnames)

Where
data is the input vector which become the data elements of the matrix.
nrow is the number of rows to be created.
ncol is the number of columns to be created.
byrow is logical clue. If TRUE then input vector elements are arranged by row.
dimnames is the names assigned to the rows and columns.

Example1:

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

How to create Matrix in R Programming

Create Matrix using colon(:) operator.

> A<-matrix(1:12,nrow=3,ncol=4) > A [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12

In the above example, matrix is filled by column wise(vertically). But, it can be filled row-wise by using byrow=TRUE argument in matrix function.

> B<-matrix(1:12,nrow=3,byrow=TRUE) > B [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12 > dim(B) #gives the dimension of matrix [1] 3 4 > class(B) #To check vector is matrix or not [1] "matrix"

It is possible to assign names to rows and columns using dimnames option in matrix() function.

> rownames=c("row1","row2","row3") > colnames=c("col1","col2","col3") C<-matrix(4:12,nrow=3,dimnames=list(rownames,colnames)) col1 col2 col3 row1 4 7 10 row2 5 8 11 row3 6 9 12

We can also create matrices using cbind() and rbind() functions:

Using cbind function

> i<-cbind(c(1,2,3),c(4,5,6)) > i [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > class(i) [1] "matrix"

Using rbind function

> j<-rbind(c(1,2,3),c(4,5,6)) > j [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 > class(j) [1] "matrix"

We can also create Matrices using dim() function.

> k<-1:10 > dim(k)<-c(2,5) > class(k) [1] "matrix" > k [,1] [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10

Access Elements of a Matrix

We can access the matrix elements by using matrix_name(row_number,col_number).

> A [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > A[3,4] # 3rd row element and 4th column element [1] 12 > A[1,] #1st Row [1] 1 4 7 10 > A[,1] #1st Column [1] 1 2 3 > A[,3] #3rd Column [1] 7 8 9 > C col1 col2 col3 row1 4 7 10 row2 5 8 11 row3 6 9 12 > > C[,"col1"] #Using dimension names row1 row2 row3 4 5 6 > C["row2",] col1 col2 col3 5 8 11

Mathematical operations on Matrices:

Different mathematical operations can be performed on Matrices. Result of matrices is also a Matrix. To perform any operation on Matrices, dimension numbers of rows and columns of matrices should be same.

> A [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > B [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12 > A+B #Addition of Matrices [,1] [,2] [,3] [,4] [1,] 2 6 10 14 [2,] 7 11 15 19 [3,] 12 16 20 24 > A-B #Subtraction [,1] [,2] [,3] [,4] [1,] 0 2 4 6 [2,] -3 -1 1 3 [3,] -6 -4 -2 0 > A*B #Multiplication [,1] [,2] [,3] [,4] [1,] 1 8 21 40 [2,] 10 30 56 88 [3,] 27 60 99 144 > A/B #Division [,1] [,2] [,3] [,4] [1,] 1.0000000 2.0000000 2.3333333 2.500 [2,] 0.4000000 0.8333333 1.1428571 1.375 [3,] 0.3333333 0.6000000 0.8181818 1.000 > A%%B #Remainder [,1] [,2] [,3] [,4] [1,] 0 0 1 2 [2,] 2 5 1 3 [3,] 3 6 9 0

Powered by WordPress