logo

MATRIX


Matrices are the R objects in which the elements are arranged in a two-dimensional rectangular layout. They contain elements of the same atomic types. Though we can create a matrix containing only characters or only logical values, they are not of much use. We use matrices containing numeric elements to be used in mathematical calculations.




A Matrix is created using the matrix() function.

Matrix are nothing but two dimensional vectors.

Each element of a matrix can be identified by its row and column position.

A=c(1,2,3,4)
dim(A)=c(2,2)
A
     [,1] [,2]
[1,]    1    3
[2,]    2    4

In the above example, A is a normal vector of size 4. Using the dim function we have converted the vector into a matrix.

matrix1=matrix(c(1,2,3,4),nrow = 2, ncol = 2)

The basic syntax for creating a matrix in R is −

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

Following is the description of the parameters used −

Remember that while creating a matrix, by default the observations are entered column wise, i.e. at first we enter all the observations under first column and then we move to 2nd column. If we want to alter this then we set byrow = TRUE.

If we want to point to a particular element of the matrix, we can do so by using matrix_name[row_number, coloumn_number]

For example:

matrix1[1,1]
[1] 1
matrix1[2,2]
[1] 4

We can even replace an element of the matrix

matrix1[1,1]=5
matrix1
     [,1] [,2]
[1,]    5    3
[2,]    2    4

Here the first element of matrix1 is replaces from 1 to 5

Matrix Calculation:

At first let us create two matrices A and B, and then we will perform some simple arithmetic on the two matrices

A = matrix(c(1,2,3,14,5,6,7,8,9),3,3)
B = matrix(c(9,8,7,6,15,4,3,2,1),3,3) 

The matrices are

A
     [,1] [,2] [,3]
[1,]    1   14    7
[2,]    2    5    8
[3,]    3    6    9
B
     [,1] [,2] [,3]
[1,]    9    6    3
[2,]    8   15    2
[3,]    7    4    1

Now let us perform some simple arithmetic

A + B   # The corresponding elements of A and B are added
     [,1] [,2] [,3]
[1,]   10   20   10
[2,]   10   20   10
[3,]   10   10   10
A - B   # The elements of B are subtracted from corresponding elemts of A
     [,1] [,2] [,3]
[1,]   -8    8    4
[2,]   -6  -10    6
[3,]   -4    2    8
A * B   # This will multiply corresponding elemets of A and B
     [,1] [,2] [,3]
[1,]    9   84   21
[2,]   16   75   16
[3,]   21   24    9
A / B   # This will divide elements of A by corresponding elements of B
          [,1]      [,2]     [,3]
[1,] 0.1111111 2.3333333 2.333333
[2,] 0.2500000 0.3333333 4.000000
[3,] 0.4285714 1.5000000 9.000000

However, in matrix multiplication or division, the process is a bit different. Hence to get actual value we use the following operation

A %*% B   # Matrix multiplication
     [,1] [,2] [,3]
[1,]  170  244   38
[2,]  114  119   24
[3,]  138  144   30

Some other functions are:

det(A)   # Determinent of A
[1] 60
t(A)     # Transpose of A
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]   14    5    6
[3,]    7    8    9
solve (A)  # Inverse of A
      [,1] [,2]       [,3]
[1,] -0.05 -1.4  1.2833333
[2,]  0.10 -0.2  0.1000000
[3,] -0.05  0.6 -0.3833333
eigen(A)  # Eigen values and eigen vector
eigen() decomposition
$values
[1] 17.3844+0.000000i -1.1922+1.424791i -1.1922-1.424791i

$vectors
             [,1]                  [,2]
[1,] 0.6582245+0i  0.9468122+0.0000000i
[2,] 0.4806063+0i -0.0254148+0.1608858i
[3,] 0.5794464+0i -0.2456849-0.1290560i
                      [,3]
[1,]  0.9468122+0.0000000i
[2,] -0.0254148-0.1608858i
[3,] -0.2456849+0.1290560i