在 R 中使用 %*% 運算子

Jesse John 2023年1月30日
  1. R 中的矩陣及其維數
  2. 使用 %*% 運算子在 R 中將矩陣相乘
  3. 使用 %*% 運算子獲取 R 中向量的點積
  4. まとめ
在 R 中使用 %*% 運算子

%*% 運算子用於矩陣乘法。在相同長度的向量中,此運算子給出點積。

在本文中,我們將通過一些簡單的示例來探索該運算子的使用。

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。

我們建立的矩陣具有以下尺寸:2x63x44x36x2

使用 %*% 運算子在 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
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.

相關文章 - R Matrix