In this R tutorial, we will discuss about matrix() functions with the different parameters. Afterwards, we will see how to perform the different arithmetic operations on matrices.

Generally, we can say that a matrix stores values in rows and columns. From this, we can say that the matrix is a 2D data structure.

## Creating Matrix in R

We can create a matrix in R by using the matrix() function.

## Syntax:

## Parameters:

- input_object is used to create a matrix . It refers to lists, vectors, etc.
- nrow stands for the number of rows that specifies the number of rows for a matrix_object.
- ncol stands for the number of columns that specifies the number of columns for a matrix_object.

## Example 1:

In this example, we will create a matrix from the vector elements.

fruits=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78)

#pass the vector to a matrix

matrix_object=matrix(fruits)

print(matrix_object)

## Result:

We just passed the input parameter, i.e. vector with 15 elements. Now, we will see the examples by passing the nrow and ncol parameters.

## Example 2:

In this example, we will create a matrix from the vector elements by specifying the nrow parameter.

fruits=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78,67)

#pass the vector to a matrix with 4 rows

matrix_object=matrix(fruits,nrow=4)

print(matrix_object)

## Result:

We just passed the input parameter, i.e. vector with 16 elements. And the matrix has 4 rows.

## Example 3:

In this example, we will create a matrix from the vector elements by specifying the ncol parameter.

fruits=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78,67)

#pass the vector to a matrix with 4 columns

matrix_object=matrix(fruits,ncol=4)

print(matrix_object)

## Result:

We just passed the input parameter, i.e. vector with 16 elements. And the matrix has 4 columns.

Now, let’s perform the arithmetic operations on the two matrices.

Here, the computation is done element wise in both the matrices.

## Example 1:

In this example, we will create two matrices with 5 rows and 4 columns and perform an addition operation.

fruits=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78,67,45,44,67,665)

#pass the vector to a matrix with 5 rows and 4 columns

matrix_object1=matrix(fruits,nrow=5,ncol=4)

#create vector for vegetables

vegetables=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78,67,87,90,87,0)

#pass the vector to a matrix with 4 rows

matrix_object2=matrix(vegetables,nrow=5,ncol=4)

print(matrix_object1)

print(matrix_object2)

print("Addition: ")

#add 2 matrices

print(matrix_object1+matrix_object2)

## Result:

We can see that the elements in the first matrix are added to the second matrix.

## Example 2:

In this example, we will create two matrices with 5 rows and 4 columns and perform a subtraction operation.

fruits=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78,67,45,44,67,665)

#pass the vector to a matrix with 5 rows and 4 columns

matrix_object1=matrix(fruits,nrow=5,ncol=4)

#create vector for vegetables

vegetables=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78,67,87,90,87,0)

#pass the vector to a matrix with 4 rows

matrix_object2=matrix(vegetables,nrow=5,ncol=4)

print(matrix_object1)

print(matrix_object2)

print("Subtraction: ")

#add 2 matrices

print(matrix_object1-matrix_object2)

## Result:

We can see that the elements in the second matrix are subtracted from the first matrix.

## Example 3:

In this example, we will create two matrices with 5 rows and 4 columns and perform a multiplication operation.

fruits=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78,67,45,44,67,665)

#pass the vector to a matrix with 5 rows and 4 columns

matrix_object1=matrix(fruits,nrow=5,ncol=4)

#create vector for vegetables

vegetables=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78,67,87,90,87,0)

#pass the vector to a matrix with 4 rows

matrix_object2=matrix(vegetables,nrow=5,ncol=4)

print(matrix_object1)

print(matrix_object2)

print("Multiplication: ")

#add 2 matrices

print(matrix_object1*matrix_object2)

## Result:

We can see that the elements from the first matrix are multiplied with the elements in the second matrix.

## Example 4:

In this example, we will create two matrices with 5 rows and 4 columns and perform a division operation.

fruits=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78,67,45,44,67,665)

#pass the vector to a matrix with 5 rows and 4 columns

matrix_object1=matrix(fruits,nrow=5,ncol=4)

#create vector for vegetables

vegetables=c(23,4,43,3,4,5,6,7,8,967,65,43,5,56,78,67,87,90,87,0)

#pass the vector to a matrix with 4 rows

matrix_object2=matrix(vegetables,nrow=5,ncol=4)

print(matrix_object1)

print(matrix_object2)

print("Division: ")

#add 2 matrices

print(matrix_object1/matrix_object2)

## Result:

We can see that the elements from the first matrix are divided by the elements in the second matrix.

## Conclusion

In this R tutorial, we saw how to create a matrix using the matrix() function. After that, we performed the different arithmetic operations like addition, subtraction, multiplication, and division.