Matrix in R - Arithmetic Operations - GeeksforGeeks

10 Feb, 2022
Arithmetic operations include addition (+), subtraction (-), multiplication(*), division (/) and modulus(%). In this article we are going to see the matrix creation and arithmetic operations on the matrices in R programming language.
Create first matrix
Syntax:
matrix_name <- matrix(data , nrow = value, ncol = value) .
Parameters:
data=includes a list/vector of elements passed as data to an matrix.
nrow= nrow represent the number of  rows  specified.
ncol= ncol represent the number of  columns  specified.
Create second matrix
Apply operation between these matrices
Display result
Addition yields the sum of the two matrices. Operator used- “+”
Example:
R
# create a vector of elementsvector1=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) # create a matrix with 4* 4 by passing this vector1matrix1 <- matrix(vector1, nrow = 4, ncol = 4)  # display matrixprint(matrix1) # create a vector of elementsvector2=c(1,2,3,2,4,5,6,3,4,1,2,7,8,9,4,5) # create a matrix with 4* 4 by passing  vector2matrix2 <- matrix(vector2, nrow = 4, ncol = 4)  # display matrixprint(matrix2) # add matricesprint(matrix1+matrix2)
 
 
Output:
 
 
Subtraction yields the difference between two matrices. The operator used: “-“.
 
Example:
 
R
# create a vector of elementsvector1=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) # create a matrix with 4* 4 by passing this vector1matrix1 <- matrix(vector1, nrow = 4, ncol = 4)  # display matrixprint(matrix1) # create a vector of elementsvector2=c(1,2,3,2,4,5,6,3,4,1,2,7,8,9,4,5) # create a matrix with 4* 4 by passing  vector2matrix2 <- matrix(vector2, nrow = 4, ncol = 4)  # display matrixprint(matrix2)print(" subtraction result") # subtract matricesprint(matrix1-matrix2)
 
 
Output:
 
 
Multiplication results in the multiplication of the elements in a matrix. The operator used: “*”
 
Example:
 
R
# create a vector of elementsvector1=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) # create a matrix with 4* 4 by passing this vector1matrix1 <- matrix(vector1, nrow = 4, ncol = 4)  # display matrixprint(matrix1) # create a vector of elementsvector2=c(1,2,3,2,4,5,6,3,4,1,2,7,8,9,4,5) # create a matrix with 4* 4 by passing  vector2matrix2 <- matrix(vector2, nrow = 4, ncol = 4)  # display matrixprint(matrix2)print(" multiplication result") # multiply matricesprint(matrix1*matrix2)
 
 
Output:
 
 
Division is used to divide element by element in the matrices. The operator used : “/”
 
Example:
 
R
# create a vector of elementsvector1=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) # create a matrix with 4* 4 by passing this vector1matrix1 <- matrix(vector1, nrow = 4, ncol = 4)  # display matrixprint(matrix1) # create a vector of elementsvector2=c(1,2,3,2,4,5,6,3,4,1,2,7,8,9,4,5) # create a matrix with 4* 4 by passing  vector2matrix2 <- matrix(vector2, nrow = 4, ncol = 4)  # display matrixprint(matrix2)print(" Division result") # divide the matricesprint(matrix1/matrix2)
 
 
Output:
 
 
Modulo returns the remainder of the elements in a matrix. The operator used: %%. The main difference between division and modulo operator is that division returns quotient and modulo returns remainder.
 
Example:
 
R
# create a vector of elementsvector1=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) # create a matrix with 4* 4 by passing this vector1matrix1 <- matrix(vector1, nrow = 4, ncol = 4)  # display matrixprint(matrix1) # create a vector of elementsvector2=c(1,2,3,2,4,5,6,3,4,1,2,7,8,9,4,5) # create a matrix with 4* 4 by passing  vector2matrix2 <- matrix(vector2, nrow = 4, ncol = 4)  # display matrixprint(matrix2)print(" modulo result") print(matrix1%%matrix2)
 
 
Output:
 
 
sagar0719kumar
Picked
R-Matrix
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
Data Visualization in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
Logistic Regression in R Programming
How to Split Column Into Multiple Columns in R DataFrame?
Control Statements in R Programming
How to import an Excel File into R ?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?