## Matrix in R

In R programming, Matrix is an object with elements arranged as a two-dimensional array like a table. An R matrix can contain elements of only the same atomic types.

In data analytics or data processing, we mostly use Matrix with the numeric datatype. So, having an hands on experience on Matrices would be helpful.

In this tutorial, we will go through tutorials that will help creating and handling Matrices in R programming.

### R Matrix – Access Elements

You can access Elements of an R matrix using the column and row index of the element.

`MatrixName[row_index, column_index]`

Let us pick element at row=3 and column=2.

```> M1
[,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12
> M1[3,2]
 7```

If row index is not mentioned and column index is mentioned, the whole column is returned.

```> M1
[,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12
> M1[,2]
 5 6 7 8```

`M1[,2]` selects second column of the matrix `M1`.

If row index is mentioned and column index is not mentioned, the whole row is returned.

```> M1
[,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12
> M1[3,]
  3  7 11```

`M1[3,]` selects thrid row of the matrix `M1`.

In R, we can add two Matrix. To add two Matrix, use addition (+) operator. The result is a matrix with the sum of the two operand Matrix.

When performing addition of two matrix, the size of two matrix, i.e., number of rows and columns should be same.

```> M1
[,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12
> M2
[,1] [,2] [,3]
[1,]    5    9   13
[2,]    6   10   14
[3,]    7   11   15
[4,]    8   12   16
> M3 = M1 + M2
> M3
[,1] [,2] [,3]
[1,]    6   14   22
[2,]    8   16   24
[3,]   10   18   26
[4,]   12   20   28```

### R Matrix Subtraction

In R, we can subtract a Matrix from other. To subtract Matrix, use subtraction (-) operator. The result is a matrix with the difference between first and second matrix.

When performing subtraction of two matrix, the size of two matrix, i.e., number of rows and columns should be same.

```> M2
[,1] [,2] [,3]
[1,]    5    9   13
[2,]    6   10   14
[3,]    7   11   15
[4,]    8   12   16
> M1
[,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12
> M3 = M2 - M1
> M3
[,1] [,2] [,3]
[1,]    4    4    4
[2,]    4    4    4
[3,]    4    4    4
[4,]    4    4    4
>```

### R Matrix Multiplication – One to One

To multiply elements of a matrix with respective elements of other matrix, use multiplication (*) operator. The multiplication happens only between the (i,j) of first matrix and (i,j) of second matrix.

```> M1
[,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12
> M2
[,1] [,2] [,3]
[1,]    5    9   13
[2,]    6   10   14
[3,]    7   11   15
[4,]    8   12   16
> M3 = M1*M2
> M3
[,1] [,2] [,3]
[1,]    5   45  117
[2,]   12   60  140
[3,]   21   77  165
[4,]   32   96  192
>```

### R Matrix Division

To divide elements of a matrix with the corresponding elements of other matrix, use division (/) operator. The multiplication happens only between the (i,j) of first matrix and (i,j) of second matrix.

```> M1
[,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12
> M2
[,1] [,2] [,3]
[1,]    5    9   13
[2,]    6   10   14
[3,]    7   11   15
[4,]    8   12   16
> M3 = M2/M1
> M3
[,1]     [,2]     [,3]
[1,] 5.000000 1.800000 1.444444
[2,] 3.000000 1.666667 1.400000
[3,] 2.333333 1.571429 1.363636
[4,] 2.000000 1.500000 1.333333
>```

### Conclusion

In this R tutorial, we have learnt about R Matrices: how to initialize an R Matrix, how to access and perform arithmetic operations on R Matrices.