📜  R中的矩阵——算术运算

📅  最后修改于: 2022-05-13 01:54:51.583000             🧑  作者: Mango

R中的矩阵——算术运算

算术运算包括加(+)、减(-)、乘(*)、除(/)和模(%)。在本文中,我们将看到 R 编程语言中矩阵的创建和算术运算。

方法

  • 创建第一个矩阵
  • 创建第二个矩阵
  • 在这些矩阵之间应用操作
  • 显示结果

添加

加法产生两个矩阵的总和。使用的运算符-“+”



例子:

R
# create a vector of elements 
vector1=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 tgis vector1
matrix1 <- matrix(vector1, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix1)
  
# create a vector of elements 
vector2=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  vector2
matrix2 <- matrix(vector2, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix2)
  
# add matrices
print(matrix1+matrix2)


R
# create a vector of elements 
vector1=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 tgis vector1
matrix1 <- matrix(vector1, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix1)
  
# create a vector of elements 
vector2=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  vector2
matrix2 <- matrix(vector2, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix2)
print(" subtraction result")
  
# subtract matrices
print(matrix1-matrix2)


R
# create a vector of elements 
vector1=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 tgis vector1
matrix1 <- matrix(vector1, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix1)
  
# create a vector of elements 
vector2=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  vector2
matrix2 <- matrix(vector2, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix2)
print(" multiplication result")
  
# multiply matrices
print(matrix1*matrix2)


R
# create a vector of elements 
vector1=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 tgis vector1
matrix1 <- matrix(vector1, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix1)
  
# create a vector of elements 
vector2=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  vector2
matrix2 <- matrix(vector2, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix2)
print(" Division result")
  
# divide the matrices
print(matrix1/matrix2)


R
# create a vector of elements 
vector1=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 tgis vector1
matrix1 <- matrix(vector1, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix1)
  
# create a vector of elements 
vector2=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  vector2
matrix2 <- matrix(vector2, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix2)
print(" modulo result")
  
print(matrix1%%matrix2)


输出:

减法

减法产生两个矩阵之间的差异。运算符使用:“-”。

例子:

电阻

# create a vector of elements 
vector1=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 tgis vector1
matrix1 <- matrix(vector1, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix1)
  
# create a vector of elements 
vector2=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  vector2
matrix2 <- matrix(vector2, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix2)
print(" subtraction result")
  
# subtract matrices
print(matrix1-matrix2)

输出:



乘法

乘法导致矩阵中元素的乘法。使用的运算符:“*”

例子:

电阻

# create a vector of elements 
vector1=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 tgis vector1
matrix1 <- matrix(vector1, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix1)
  
# create a vector of elements 
vector2=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  vector2
matrix2 <- matrix(vector2, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix2)
print(" multiplication result")
  
# multiply matrices
print(matrix1*matrix2)

输出:

分配

除法用于将矩阵中的元素逐个划分。使用的运算符:“/”

例子:

电阻

# create a vector of elements 
vector1=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 tgis vector1
matrix1 <- matrix(vector1, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix1)
  
# create a vector of elements 
vector2=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  vector2
matrix2 <- matrix(vector2, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix2)
print(" Division result")
  
# divide the matrices
print(matrix1/matrix2)

输出:

模运算

取模返回矩阵中元素的剩余部分。使用的运算符:%%。除法和取模运算符之间的主要区别在于除法返回商而取模返回余数。

例子:

电阻

# create a vector of elements 
vector1=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 tgis vector1
matrix1 <- matrix(vector1, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix1)
  
# create a vector of elements 
vector2=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  vector2
matrix2 <- matrix(vector2, nrow = 4, ncol = 4)  
  
# display matrix
print(matrix2)
print(" modulo result")
  
print(matrix1%%matrix2)

输出: