📜  在 R 中将矩阵乘以向量

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

在 R 中将矩阵乘以向量

矩阵是二维结构,而向量是一维结构。在本文中,我们将使用 R 编程语言将给定的矩阵乘以给定的向量。当向量元素与矩阵元素按列相乘时,两者之间会发生相乘。

方法:

  • 创建矩阵
  • 创建一个向量
  • 将它们相乘
  • 显示结果。

方法一:朴素方法

结构准备好后,我们直接使用乘法运算符(*) 将它们相乘。

例子:



R
# create a vector for matrix elements
vector1=c(1,2,3,4,5,6,7,8,9,10,11,12)
  
# Create A matrix with 2 rows and 6 columns
matrix1 <- matrix(vector1, nrow=2,ncol=6) 
  
# multiplication vector
mul_vec=c(1,2,3,4)
  
# print multiplication result
print(matrix1*mul_vec)


R
# create a vector for matrix elements
vector1=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
  
# Create A matrix with 4 rows and 4 columns
matrix1 <- matrix(vector1, nrow=4,ncol=4) 
print(matrix1)
  
mul_vec=c(1,2,3,4)
  
print("Result")
print(matrix1*mul_vec)


R
# create a vector for matrix elements
vector1=c(1,2,3,4)
  
# Create A matrix with 2 rows and 2 columns
matrix1 <- matrix(vector1,nrow=2,ncol=2) 
print(matrix1)
  
mul_vec=c(1,2,3,4)
  
print("Result")
print(matrix1*mul_vec)


R
# create matrix with 15 elements
matrix1 <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15),
                  nrow=3,byrow=TRUE)
print(matrix1)
print("---------------")
  
# create a vector
vector1 <- c(1,2,3,4,5)
  
# apply sweep operation that multiplies row 
# wise(margin=2)
print(sweep(matrix1, MARGIN=2,vector1, `*`))
print("---------------")
  
# create elements with vector 2
vector2 <- c(1,2,3)
  
# apply sweep operation that multiplies column
# wise with vector 2(margin=1)
print(sweep(matrix1, MARGIN=1,vector2, `*`))


R
# create matrix with 8 elements
matrix1 <- matrix(c(1,2,3,4,5,6,7,8),
                  nrow=2,byrow=TRUE)
print(matrix1)
print("---------------")
  
# create a vector
vector1 <- c(1,2,3,4)
  
# apply sweep operation that multiplies 
# row wise(margin=2)
print(sweep(matrix1, MARGIN=2,vector1, `*`))
print("---------------")
  
# create elements with vector 2
vector2 <- c(1,2)
  
# apply sweep operation that multiplies column 
# wise with vector 2(margin=1)
print(sweep(matrix1, MARGIN=1,vector2, `*`))


输出:

示例 2:

电阻

# create a vector for matrix elements
vector1=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
  
# Create A matrix with 4 rows and 4 columns
matrix1 <- matrix(vector1, nrow=4,ncol=4) 
print(matrix1)
  
mul_vec=c(1,2,3,4)
  
print("Result")
print(matrix1*mul_vec)

输出:

示例 3:



此代码具有矩阵和向量的大小相等

电阻

# create a vector for matrix elements
vector1=c(1,2,3,4)
  
# Create A matrix with 2 rows and 2 columns
matrix1 <- matrix(vector1,nrow=2,ncol=2) 
print(matrix1)
  
mul_vec=c(1,2,3,4)
  
print("Result")
print(matrix1*mul_vec)

输出:

方法二:使用sweep()

我们可以使用sweep() 方法将向量乘以矩阵。 scan()函数用于将操作“+ 或 – 或 '*' 或 '/' ”应用于给定矩阵中的行或列。

在这里,我们正在执行“*”操作



例子:

电阻

# create matrix with 15 elements
matrix1 <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15),
                  nrow=3,byrow=TRUE)
print(matrix1)
print("---------------")
  
# create a vector
vector1 <- c(1,2,3,4,5)
  
# apply sweep operation that multiplies row 
# wise(margin=2)
print(sweep(matrix1, MARGIN=2,vector1, `*`))
print("---------------")
  
# create elements with vector 2
vector2 <- c(1,2,3)
  
# apply sweep operation that multiplies column
# wise with vector 2(margin=1)
print(sweep(matrix1, MARGIN=1,vector2, `*`))

输出:

示例 2:

电阻

# create matrix with 8 elements
matrix1 <- matrix(c(1,2,3,4,5,6,7,8),
                  nrow=2,byrow=TRUE)
print(matrix1)
print("---------------")
  
# create a vector
vector1 <- c(1,2,3,4)
  
# apply sweep operation that multiplies 
# row wise(margin=2)
print(sweep(matrix1, MARGIN=2,vector1, `*`))
print("---------------")
  
# create elements with vector 2
vector2 <- c(1,2)
  
# apply sweep operation that multiplies column 
# wise with vector 2(margin=1)
print(sweep(matrix1, MARGIN=1,vector2, `*`))

输出: