R语言 对矩阵的操作

R语言 对矩阵的操作

R中的矩阵是一堆数值,可以是实数也可以是复数,排列在一组固定数量的行和列中。矩阵被用来以一种结构化和组织良好的格式来描述数据。有必要用小括号或大括号将矩阵中的元素括起来。一个有9个元素的矩阵如下所示。

R语言 对矩阵的操作

这个矩阵[M]有3行3列。矩阵[M]的每个元素都可以用其行号和列号来表示。例如,a23 = 6

矩阵的顺序: 矩阵的顺序是根据其行数和列数来定义的。矩阵的阶数=行数×列数 因此矩阵[M]是一个阶数为3×3的矩阵。

矩阵的操作

有四种基本操作,即DMAS(除法、乘法、加法、减法),可以对矩阵进行操作。操作中涉及的两个矩阵的行数和列数都应相同。

矩阵加法

两个相同的有序矩阵R语言 对矩阵的操作R语言 对矩阵的操作相加,得到一个矩阵R语言 对矩阵的操作,其中每个元素都是输入矩阵中相应元素的和。

# 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的元素是通过嵌套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,] 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语言 对矩阵的操作R语言 对矩阵的操作相减,得到一个矩阵R语言 对矩阵的操作,其中每个元素都是第二个输入矩阵的相应元素与第一个矩阵的差值。

# 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语言 对矩阵的操作R语言 对矩阵的操作相乘,得到一个矩阵R语言 对矩阵的操作,其中每个元素都是输入矩阵中相应元素的乘积。

# 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

通过嵌套的for循环,sum的元素是B和C的相应元素的乘法。 使用*运算符进行矩阵乘法:同样,下面的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(BC)=(AB)C
  • 所涉及的矩阵的顺序必须相同。

矩阵除法

两个相同的有序矩阵R语言 对矩阵的操作R语言 对矩阵的操作,得到一个矩阵R语言 对矩阵的操作,其中每个元素都是第一个矩阵元素的相应元素的商除以第二个。

# 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
  • 所涉矩阵的顺序必须相同。

注: 所有矩阵操作的时间复杂度=O(r*c),其中r*c是矩阵的顺序。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程