In R programming, a 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. In this tutorial, we will deal with Matrix containing numbers.

## Create a Matrix in R

matrix() function is used to create a Matrix in R. Following is the syntax of matrix() function.

where

• `data` is the input vector which becomes the data elements of the matrix. `NA` is the default value.
• `nrow` denotes the number of rows to be created. `1` is the default value.
• `ncol` specifies the number of columns to be created. `1` is the default value.
• If `byrow` is TRUE then the input vector elements are arranged by row. If `byrow` is FALSE, the input vector elements are arranged by column.
• `dimname` is the names assigned to the rows and columns like in an Excel file.

### Example to create an R matrix

In this example, we will create a matrix with a vector of numbers as input. A vector of numbers is of fixed length. If we do not specify any number for rows or columns, the vector of numbers is taken as a single column.

Let us specify a number for `nrow`, i.e., number of rows.

Total number of elements in the Vector = 12.

So, when we specified number of rows = 3,

Number of columns = (total number of elements)/nrow = 12/3 = 4

Instead of specifying `nrow`, we can specify `ncol` while creating an R matrix.

The same explanation as of `nrow` holds for `ncol` as well.

If you specify a value for nrow or ncol which does not exactly divide the total number of elements, you will get an error saying that “data length is not a sub-multiple or multiple of the number of columns”

Now let us see how byrow affects the creation of the Matrix with this example.

While creating matrix M1, `byrow=FALSE`. Which means the vector of numbers are arranged by column. The sequence is column wise.

While creating matrix M2, `byrow=TRUE`. Which means the vector of numbers are arranged by row. The sequence is row wise.

Now its time to know how to use `dimnames`. We can specify both row names and column names. So we will take two vectors, one for rows and one for columns.

## Access Elements of R Matrix

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

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

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

`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[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.

## 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.

## R Matrix Multiplication

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

## 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.