R中的矩阵运算
R 中的矩阵是一组值,可以是实数或复数,排列在一组固定数量的行和列中。矩阵用于以结构化和组织良好的格式描述数据。
必须将矩阵的元素括在圆括号或方括号中。
一个包含 9 个元素的矩阵如下所示。
这个矩阵 [M] 有 3 行和 3 列。矩阵 [M] 的每个元素都可以通过其行号和列号来引用。例如, 23 = 6
矩阵的顺序:
矩阵的顺序是根据其行数和列数来定义的。
矩阵的阶数 = 行数 × 列数
因此 Matrix [M] 是一个 3 × 3 阶矩阵。
矩阵运算
矩阵有四种基本运算,即 DMAS(除法、乘法、加法、减法)。操作中涉及的两个矩阵应该具有相同的行数和列数。
矩阵加法
两个相同的有序矩阵相加和产生一个矩阵其中每个元素是输入矩阵的相应元素的总和。
# R program to add two matrices
# Creating 1st Matrix
B = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3)
# Getting number of rows and columns
num_of_rows = nrow(B)
num_of_cols = ncol(B)
# Creating matrix to store results
sum = matrix(, nrow = num_of_rows, ncol = num_of_cols)
# Printing Original matrices
print(B)
print(C)
输出:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
[,1] [,2] [,3]
[1,] 8 12 16
[2,] 10 14 18
在上面的代码中,nrow(B) 给出了 B 中的行数,ncol(B) 给出了列数。这里,sum是一个与B和C大小相同的空矩阵。sum的元素是B和C的对应元素通过嵌套for循环相加。
使用“+”运算符进行矩阵加法:
同样,以下 R 脚本使用内置运算符+:
# R program for matrix addition
# using '+' operator
# Creating 1st Matrix
B = matrix(c(1, 2 + 3i, 5.4, 3, 4, 5), nrow = 2, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(2, 0i, 0.1, 3, 4, 5), nrow = 2, ncol = 3)
# Printing the resultant matrix
print(B + C)
输出:
[,1] [,2] [,3]
[1,] 3+0i 5.5+0i 8+0i
[2,] 2+3i 6.0+0i 10+0i
R 提供了基本的内置运算符来添加矩阵。在上面的代码中,结果矩阵中的所有元素都作为复数返回,即使矩阵的单个元素是复数。
矩阵加法的性质:
- 交换: B + C = C + B
- 关联:对于 n 个矩阵 A + (B + C) = (A + B) + C
- 涉及的矩阵的顺序必须相同。
矩阵减法
两个相同有序矩阵的减法和产生一个矩阵其中每个元素是第二个输入矩阵的对应元素与第一个输入矩阵的差异。
# R program to add two matrices
# Creating 1st Matrix
B = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3)
# Getting number of rows and columns
num_of_rows = nrow(B)
num_of_cols = ncol(B)
# Creating matrix to store results
diff = matrix(, nrow = num_of_rows, ncol = num_of_cols)
# Printing Original matrices
print(B)
print(C)
# Calculating diff of matrices
for(row in 1:num_of_rows)
{
for(col in 1:num_of_cols)
{
diff[row, col] <- B[row, col] - C[row, col]
}
}
# Printing resultant matrix
print(diff)
输出:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
[,1] [,2] [,3]
[1,] -6 -6 -6
[2,] -6 -6 -6
这里在上面的代码中,diff矩阵的元素是通过嵌套的for循环减去B和C的对应元素。
使用“-”运算符进行矩阵减法:
同样,以下 R 脚本使用内置运算符“-”:
# R program for matrix addition
# using '-' operator
# Creating 1st Matrix
B = matrix(c(1, 2 + 3i, 5.4, 3, 4, 5), nrow = 2, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(2, 0i, 0.1, 3, 4, 5), nrow = 2, ncol = 3)
# Printing the resultant matrix
print(B - C)
输出:
[,1] [,2] [,3]
[1,] -1+0i 5.3+0i 0+0i
[2,] 2+3i 0.0+0i 0+0i
矩阵减法的性质:
- 不可交换: B – C != C – B
- 非关联:对于 n 个矩阵 A – (B – C) != (A – B) – C
- 涉及的矩阵的顺序必须相同。
矩阵乘法
两个相同有序矩阵的乘法和产生一个矩阵其中每个元素都是输入矩阵的相应元素的乘积。
# R program to multiply two matrices
# Creating 1st Matrix
B = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3)
# Getting number of rows and columns
num_of_rows = nrow(B)
num_of_cols = ncol(B)
# Creating matrix to store results
prod = matrix(, nrow = num_of_rows, ncol = num_of_cols)
# Printing Original matrices
print(B)
print(C)
# Calculating product of matrices
for(row in 1:num_of_rows)
{
for(col in 1:num_of_cols)
{
prod[row, col] <- B[row, col] * C[row, col]
}
}
# Printing resultant matrix
print(prod)
输出:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
[,1] [,2] [,3]
[1,] 7 27 55
[2,] 16 40 72
sum 的元素是 B 和 C 的对应元素通过嵌套的 for 循环相乘。
使用 '*'运算符进行矩阵乘法:
同样,以下 R 脚本使用内置运算符*:
# R program for matrix multiplication
# using '*' operator
# Creating 1st Matrix
B = matrix(c(1, 2 + 3i, 5.4), nrow = 1, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(2, 1i, 0.1), nrow = 1, ncol = 3)
# Printing the resultant matrix
print (B * C)
输出:
[,1] [,2] [,3]
[1,] 2+0i -3+2i 0.54+0i
矩阵乘法的性质:
- 交换: B * C = C * B
- 关联:对于 n 个矩阵 A * (B * C) = (A * B) * C
- 涉及的矩阵的顺序必须相同。
矩阵事业部
两个相同有序矩阵的除法和产生一个矩阵其中每个元素是第一个矩阵元素的相应元素除以第二个矩阵元素的商。
# R program to divide two matrices
# Creating 1st Matrix
B = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(7, 8, 9, 10, 11, 12), nrow = 2, ncol = 3)
# Getting number of rows and columns
num_of_rows = nrow(B)
num_of_cols = ncol(B)
# Creating matrix to store results
div = matrix(, nrow = num_of_rows, ncol = num_of_cols)
# Printing Original matrices
print(B)
print(C)
# Calculating product of matrices
for(row in 1:num_of_rows)
{
for(col in 1:num_of_cols)
{
div[row, col] <- B[row, col] / C[row, col]
}
}
# Printing resultant matrix
print(div)
输出:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
[,1] [,2] [,3]
[1,] 0.1428571 0.3333333 0.4545455
[2,] 0.2500000 0.4000000 0.5000000
div 矩阵的元素是通过嵌套的 for 循环对 B 和 C 的对应元素进行划分。
使用 '/'运算符进行矩阵除法:
同样,以下 R 脚本使用内置运算符/:
# R program for matrix division
# using '/' operator
# Creating 1st Matrix
B = matrix(c(4, 6i, -1), nrow = 1, ncol = 3)
# Creating 2nd Matrix
C = matrix(c(2, 2i, 0), nrow = 1, ncol = 3)
# Printing the resultant matrix
print (B / C)
输出:
[,1] [,2] [,3]
[1,] 2+0i 3+0i -Inf+NaNi
矩阵除法的性质:
- 不可交换: B / C != C / B
- 非关联:对于 n 个矩阵 A / (B / C) != (A / B) / C
- 涉及的矩阵的顺序必须相同。
Note: Time Complexity of all the matrix operations = O(r*c) where r*c is the order of the matrix.
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。