用R中的向量元素划分矩阵的每一行
在本文中,我们将讨论如何在 R 编程语言中用向量元素划分矩阵的每一行。
方法一:使用标准除法
最初,计算矩阵的转置,以交换行和列。最初,如果矩阵的维度为 n * m ,则转置将维度转换为 m * n。需要计算矩阵的转置,因为布尔除法运算符“/”是按列应用的,我们需要计算按行除法。然后使用转置矩阵作为一个操作数和向量作为另一个应用除法运算。然后对该结果进行转置,以再次保留行和列的顺序。
句法:
t(transpose_matrix/vector)
例子:
R
# creating matrix
matrix <- matrix(1:12,ncol=3)
print ("Original Matrix")
print (matrix)
# creating vector
vec <- c(1:3)
# transpose matrix
trans_mat <- t(matrix)
# computing division
div <- t(trans_mat/vec)
print ("Division matrix")
print (div)
R
# creating matrix
matrix <- matrix(1:12,ncol=3)
print ("Original Matrix")
print (matrix)
# creating vector
vec <- c(1:3)
# computing division
div <- sweep(matrix, 2, vec, "/")
print ("Division matrix")
print (div)
R
# creating matrix
matrix <- matrix(1:16,ncol=2)
print ("Original Matrix")
print (matrix)
# creating vector
vec <- c(1:2)
# calculating rows
rows <- nrow(matrix)
# computing division
div <- matrix / rep(vec, each = rows)
print ("Division matrix")
print (div)
R
# creating matrix
matrix <- matrix(1:16,ncol=2)
print ("Original Matrix")
print (matrix)
# creating vector
vec <- c(1:2)
# calculating rows
rows <- nrow(matrix)
# computing division
div <- t(apply(matrix, 1, "/", vec))
print ("Division matrix")
print (div)
R
# creating matrix
matrix <- matrix(1:16,ncol=2)
print ("Original Matrix")
print (matrix)
# creating vector
vec <- c(1:2)
# calculating rows
rows <- nrow(matrix)
# computing division
div <- matrix %*% diag(1 / vec)
print ("Division matrix")
print (div)
输出
[1] "Original Matrix"
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
[1] "Division matrix"
[,1] [,2] [,3]
[1,] 1 2.5 3.000000
[2,] 2 3.0 3.333333
[3,] 3 3.5 3.666667
[4,] 4 4.0 4.000000
方法二:使用sweep()方法
R 语言中的此方法通过扫出汇总统计返回从输入数组获得的数组。该方法用于计算选定轴上数据帧的算术运算。对于逐行操作,所选轴为 2,操作数成为数据框的行。结果必须存储在另一个变量中。此操作所花费的时间相当于数据帧中的行数。结果列的数据类型是最大的兼容数据类型。
Syntax: sweep (df , axis, vec, op)
Parameter :
- df – DataFrame
- axis – To compute it row-wise, use axis = 1 and for column-wise, use axis = 2
- vec – The vector to apply on the data frame
- op – The operator to apply
例子:
电阻
# creating matrix
matrix <- matrix(1:12,ncol=3)
print ("Original Matrix")
print (matrix)
# creating vector
vec <- c(1:3)
# computing division
div <- sweep(matrix, 2, vec, "/")
print ("Division matrix")
print (div)
输出
[1] "Original Matrix"
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
[1] "Division matrix"
[,1] [,2] [,3]
[1,] 1 2.5 3.000000
[2,] 2 3.0 3.333333
[3,] 3 3.5 3.666667
[4,] 4 4.0 4.000000
方法 3:使用rep()方法
R 中的 rep(x) 方法用于复制向量 x 中的值。它将“每个”参数作为参数,其中每个元素重复每次多次。 rep()函数数值、文本或向量的值复制特定次数。
Syntax: rep ( vec, each = )
Parameter :
- vec : The vector whose value is replicated.
- each : non-negative integer. Other inputs will be coerced to an integer or double vector and the first element taken.
这里应用 rep() 方法的想法是创建向量的副本并将其堆叠在一起,以创建与行数相等的副本数。接下来是对相关矩阵的划分。
例子:
电阻
# creating matrix
matrix <- matrix(1:16,ncol=2)
print ("Original Matrix")
print (matrix)
# creating vector
vec <- c(1:2)
# calculating rows
rows <- nrow(matrix)
# computing division
div <- matrix / rep(vec, each = rows)
print ("Division matrix")
print (div)
输出
[1] "Original Matrix"
[,1] [,2]
[1,] 1 9
[2,] 2 10
[3,] 3 11
[4,] 4 12
[5,] 5 13
[6,] 6 14
[7,] 7 15
[8,] 8 16
[1] "Division matrix"
[,1] [,2]
[1,] 1 4.5
[2,] 2 5.0
[3,] 3 5.5
[4,] 4 6.0
[5,] 5 6.5
[6,] 6 7.0
[7,] 7 7.5
[8,] 8 8.0
方法 4:使用 apply() 方法
apply() 方法是一种集合方法,用于对整个指定对象应用转换。 apply() 方法将数据框或矩阵作为输入,并以向量、列表或数组的形式给出输出。
Syntax: apply(matrix , axis , FUN)
Parameter :
- matrix : an array or matrix
- axis : indicator of the axis over which transformation is applied
- axis =1 : row-wise manipulation
- axis =2 : column-wise manipulation
- axis=c(1,2) : the manipulation is performed on rows and columns
- FUN: tells which function to apply.
在应用 apply() 方法之后,必须计算结果的转置以保持顺序,因为 apply() 方法返回转置矩阵。
例子:
电阻
# creating matrix
matrix <- matrix(1:16,ncol=2)
print ("Original Matrix")
print (matrix)
# creating vector
vec <- c(1:2)
# calculating rows
rows <- nrow(matrix)
# computing division
div <- t(apply(matrix, 1, "/", vec))
print ("Division matrix")
print (div)
输出
[1] "Original Matrix"
[,1] [,2]
[1,] 1 9
[2,] 2 10
[3,] 3 11
[4,] 4 12
[5,] 5 13
[6,] 6 14
[7,] 7 15
[8,] 8 16
[1] "Division matrix"
[,1] [,2]
[1,] 1 4.5
[2,] 2 5.0
[3,] 3 5.5
[4,] 4 6.0
[5,] 5 6.5
[6,] 6 7.0
[7,] 7 7.5
[8,] 8 8.0
方法 5:使用 %*%运算符
%*%运算符是一种特殊的乘法运算符,定义用于矩阵乘法。此运算符用于将矩阵与其转置相乘。最初,使用 R 中的 diag()函数计算指定向量的对角矩阵。它将向量的逆作为参数,然后将此矩阵与原始矩阵相乘以产生除法。这消除了显式除法的需要,因为已经考虑了逆。
Syntax: diag( x )
Parameter :
x: vector to be present as the diagonal elements.
例子:
电阻
# creating matrix
matrix <- matrix(1:16,ncol=2)
print ("Original Matrix")
print (matrix)
# creating vector
vec <- c(1:2)
# calculating rows
rows <- nrow(matrix)
# computing division
div <- matrix %*% diag(1 / vec)
print ("Division matrix")
print (div)
输出
[1] "Original Matrix"
[,1] [,2]
[1,] 1 9
[2,] 2 10
[3,] 3 11
[4,] 4 12
[5,] 5 13
[6,] 6 14
[7,] 7 15
[8,] 8 16
[1] "Division matrix"
[,1] [,2]
[1,] 1 4.5
[2,] 2 5.0
[3,] 3 5.5
[4,] 4 6.0
[5,] 5 6.5
[6,] 6 7.0
[7,] 7 7.5
[8,] 8 8.0