在 R 中使用 %*% 运算符
Jesse John
2023年1月30日
%*%
运算符用于矩阵乘法。在相同长度的向量中,此运算符给出点积。
在本文中,我们将通过一些简单的示例来探索该运算符的使用。
R 中的矩阵及其维数
矩阵是数字的矩形数组。它就像一个数字表,有行和列。
以下代码使用相同的 12 个数字创建并显示四个矩阵。
示例代码:
# First, we will create a vector of numbers.
# These 12 numbers are what we will place in our matrices.
v = 7:18
# Matrix with 2 rows and 6 columns.
matrix(v, nrow=2)
dim(matrix(v, nrow=2))
# Matrix with 3 rows and 4 columns.
matrix(v, nrow=3)
dim(matrix(v, nrow=3))
# Matrix with 4 rows and 3 columns.
matrix(v, nrow=4)
dim(matrix(v, nrow=4))
# Matrix with 6 rows and 2 columns.
matrix(v, nrow=6)
dim(matrix(v, nrow=6))
输出:
> # Matrix with 2 rows and 6 columns.
> matrix(v, nrow=2)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 7 9 11 13 15 17
[2,] 8 10 12 14 16 18
> dim(matrix(v, nrow=2))
[1] 2 6
> # Matrix with 3 rows and 4 columns.
> matrix(v, nrow=3)
[,1] [,2] [,3] [,4]
[1,] 7 10 13 16
[2,] 8 11 14 17
[3,] 9 12 15 18
> dim(matrix(v, nrow=3))
[1] 3 4
> # Matrix with 4 rows and 3 columns.
> matrix(v, nrow=4)
[,1] [,2] [,3]
[1,] 7 11 15
[2,] 8 12 16
[3,] 9 13 17
[4,] 10 14 18
> dim(matrix(v, nrow=4))
[1] 4 3
> # Matrix with 6 rows and 2 columns.
> matrix(v, nrow=6)
[,1] [,2]
[1,] 7 13
[2,] 8 14
[3,] 9 15
[4,] 10 16
[5,] 11 17
[6,] 12 18
> dim(matrix(v, nrow=6))
[1] 6 2
我们在上面创建的每个矩阵都有不同的行数和列数。
矩阵由其行数和列数描述;这称为它的维度。具有 m
行和 n
列的矩阵称为 m x n
矩阵,读作 m × n。
我们创建的矩阵具有以下尺寸:2x6
、3x4
、4x3
和 6x2
。
使用 %*%
运算符在 R 中将矩阵相乘
仅当第一个矩阵的列数等于第二个矩阵的行数时才定义矩阵乘法。当满足这个条件时,我们可以使用 %*%
运算符按顺序将这两个矩阵相乘,并且乘积也是一个矩阵。
乘积矩阵的行数与第一个矩阵一样多,列数与第二个矩阵一样多。
示例代码:
# First, we will create two matrices for which multiplication is defined.
Ist = matrix(v, ncol=3)
Ist
IInd = matrix(v, nrow=3)
IInd
# Find the product matrix.
Ist %*% IInd
输出:
> # First, we will create two matrices for which multiplication is defined.
> Ist = matrix(v, ncol=3)
> Ist
[,1] [,2] [,3]
[1,] 7 11 15
[2,] 8 12 16
[3,] 9 13 17
[4,] 10 14 18
> IInd = matrix(v, nrow=3)
> IInd
[,1] [,2] [,3] [,4]
[1,] 7 10 13 16
[2,] 8 11 14 17
[3,] 9 12 15 18
> # Find the product matrix.
> Ist %*% IInd
[,1] [,2] [,3] [,4]
[1,] 272 371 470 569
[2,] 296 404 512 620
[3,] 320 437 554 671
[4,] 344 470 596 722
我们将看另一个有效矩阵乘法的示例和两个未定义矩阵乘法的示例。
示例代码:
# A 3 x 2 matrix.
IInd_b = matrix(20:25, nrow=3)
IInd_b
# A 2 x 6 matrix.
Ist_b = matrix(v, nrow=2)
Ist_b
# Matrix multiplication is defined between Ist and IInd_b.
Ist %*% IInd_b
# Multiplication is NOT defined in the following two cases.
IInd_b %*% Ist
Ist_b %*% IInd_b
输出:
> # A 3 x 2 matrix.
> IInd_b = matrix(20:25, nrow=3)
> IInd_b
[,1] [,2]
[1,] 20 23
[2,] 21 24
[3,] 22 25
> # A 2 x 6 matrix.
> Ist_b = matrix(v, nrow=2)
> Ist_b
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 7 9 11 13 15 17
[2,] 8 10 12 14 16 18
> # Matrix multiplication is defined between Ist and IInd_b.
> Ist %*% IInd_b
[,1] [,2]
[1,] 701 800
[2,] 764 872
[3,] 827 944
[4,] 890 1016
> # Multiplication is NOT defined in the following two cases.
> IInd_b %*% Ist
Error in IInd_b %*% Ist : non-conformable arguments
> Ist_b %*% IInd_b
Error in Ist_b %*% IInd_b : non-conformable arguments
使用 %*%
运算符获取 R 中向量的点积
向量由它们的长度和类别(和类型)来描述。
示例代码:
# Create a vector.
vtr = c(11,22,33)
# Check that it is a vector.
is.vector(vtr)
# Length of the vector.
length(vtr)
# Class of the vector.
class(vtr)
# Type of the vector.
typeof(vtr)
输出:
> # Create a vector.
> vtr = c(11,22,33)
> # Check that it is a vector.
> is.vector(vtr)
[1] TRUE
> # Length of the vector.
> length(vtr)
[1] 3
> # Class of the vector.
> class(vtr)
[1] "numeric"
> # Type of the vector.
> typeof(vtr)
[1] "double"
向量的长度是其中元素(数字)的数量。
当我们使用 %*%
运算符将两个相同长度的向量相乘时,我们得到向量的点积。R 隐含地将第一个向量视为行矩阵,将第二个向量视为列矩阵,并为我们提供乘积矩阵。
它返回一个 1x1
矩阵而不是一个标量。我们可以使用 is.vector()
和 is.matrix()
函数来验证这一点。
在下面的代码中,我们将首先获得两个相同长度的向量之间的点积。然后,我们将使用一致维度的矩阵得到相同的结果。
示例代码:
# Four-element vectors.
V_I = 22:25
V_II = 2:5
# Dot product of vectors of the same dimension.
V_I %*% V_II
# Check the input and output.
is.vector(V_I)
is.matrix(V_I)
is.vector(V_I %*% V_II)
is.matrix(V_I %*% V_II)
# Create matrices of conformable dimensions (where matrix multiplication is defined).
m_I = matrix(V_I, nrow=1)
m_I
m_II = matrix(V_II, ncol=1)
m_II
# Matrix product.
m_I %*% m_II
输出:
> # Four-element vectors.
> V_I = 22:25
> V_II = 2:5
> # Dot product of vectors of the same dimension.
> V_I %*% V_II
[,1]
[1,] 334
> # Check the input and output.
> is.vector(V_I)
[1] TRUE
> is.matrix(V_I)
[1] FALSE
> is.vector(V_I %*% V_II)
[1] FALSE
> is.matrix(V_I %*% V_II)
[1] TRUE
> # Create matrices of conformable dimensions (where matrix multiplication is defined).
> m_I = matrix(V_I, nrow=1)
> m_I
[,1] [,2] [,3] [,4]
[1,] 22 23 24 25
> m_II = matrix(V_II, ncol=1)
> m_II
[,1]
[1,] 2
[2,] 3
[3,] 4
[4,] 5
> # Matrix product.
> m_I %*% m_II
[,1]
[1,] 334
如果向量的长度不同,我们就无法计算点积。
示例代码:
# A three-element vector.
V_II_b = 6:8
# Dot product is not possible.
V_I %*% V_II_b
输出:
> # A three-element vector.
> V_II_b = 6:8
> # Dot product is not possible.
> V_I %*% V_II_b
Error in V_I %*% V_II_b : non-conformable arguments
结论
对于乘法的一致矩阵,%*%
返回乘积矩阵。对于相同长度的向量,它将点积作为 1x1
矩阵返回。
作者: Jesse John