课程表

R语言 基础教程

R语言 图表

R语言 数据接口

R语言 统计示例

工具箱
速查手册

R语言 矩阵

当前位置:免费教程 » 程序设计 » R语言

矩阵是其中元素以二维矩形布局布置的R对象。 它们包含相同原子类型的元素。 虽然我们可以创建一个只包含字符或只包含逻辑值的矩阵,但它们没有太多用处。 我们使用包含数字元素的矩阵用于数学计算。

使用matrix()函数创建一个矩阵。

语法

在R语言中创建矩阵的基本语法是 -

  1. matrix(data, nrow, ncol, byrow, dimnames)

以下是所使用的参数的说明 -

  • 数据是成为矩阵的数据元素的输入向量。

  • nrow是要创建的行数。

  • ncol是要创建的列数。

  • byrow是一个逻辑线索。 如果为TRUE,则输入向量元素按行排列。

  • dimname是分配给行和列的名称。

创建一个以数字向量作为输入的矩阵

  1. # Elements are arranged sequentially by row.
  2. M <- matrix(c(3:14), nrow = 4, byrow = TRUE)
  3. print(M)
  4.  
  5. # Elements are arranged sequentially by column.
  6. N <- matrix(c(3:14), nrow = 4, byrow = FALSE)
  7. print(N)
  8.  
  9. # Define the column and row names.
  10. rownames = c("row1", "row2", "row3", "row4")
  11. colnames = c("col1", "col2", "col3")
  12.  
  13. P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
  14. print(P)

当我们执行上面的代码,它产生以下结果 -

  1. [,1] [,2] [,3]
  2. [1,] 3 4 5
  3. [2,] 6 7 8
  4. [3,] 9 10 11
  5. [4,] 12 13 14
  6. [,1] [,2] [,3]
  7. [1,] 3 7 11
  8. [2,] 4 8 12
  9. [3,] 5 9 13
  10. [4,] 6 10 14
  11. col1 col2 col3
  12. row1 3 4 5
  13. row2 6 7 8
  14. row3 9 10 11
  15. row4 12 13 14

访问矩阵的元素

可以通过使用元素的列和行索引来访问矩阵的元素。 我们考虑上面的矩阵P找到下面的具体元素。

  1. # Define the column and row names.
  2. rownames = c("row1", "row2", "row3", "row4")
  3. colnames = c("col1", "col2", "col3")
  4.  
  5. # Create the matrix.
  6. P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))
  7.  
  8. # Access the element at 3rd column and 1st row.
  9. print(P[1,3])
  10.  
  11. # Access the element at 2nd column and 4th row.
  12. print(P[4,2])
  13.  
  14. # Access only the 2nd row.
  15. print(P[2,])
  16.  
  17. # Access only the 3rd column.
  18. print(P[,3])

当我们执行上面的代码,它产生以下结果 -

  1. [1] 5
  2. [1] 13
  3. col1 col2 col3
  4. 6 7 8
  5. row1 row2 row3 row4
  6. 5 8 11 14

矩阵计算

使用R运算符对矩阵执行各种数学运算。 操作的结果也是一个矩阵。
对于操作中涉及的矩阵,维度(行数和列数)应该相同。

矩阵加法和减法

  1. # Create two 2x3 matrices.
  2. matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
  3. print(matrix1)
  4.  
  5. matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
  6. print(matrix2)
  7.  
  8. # Add the matrices.
  9. result <- matrix1 + matrix2
  10. cat("Result of addition","
  11. ")
  12. print(result)
  13.  
  14. # Subtract the matrices
  15. result <- matrix1 - matrix2
  16. cat("Result of subtraction","
  17. ")
  18. print(result)

当我们执行上面的代码,它产生以下结果 -

  1. [,1] [,2] [,3]
  2. [1,] 3 -1 2
  3. [2,] 9 4 6
  4. [,1] [,2] [,3]
  5. [1,] 5 0 3
  6. [2,] 2 9 4
  7. Result of addition
  8. [,1] [,2] [,3]
  9. [1,] 8 -1 5
  10. [2,] 11 13 10
  11. Result of subtraction
  12. [,1] [,2] [,3]
  13. [1,] -2 -1 -1
  14. [2,] 7 -5 2

矩阵乘法和除法

  1. # Create two 2x3 matrices.
  2. matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
  3. print(matrix1)
  4.  
  5. matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
  6. print(matrix2)
  7.  
  8. # Multiply the matrices.
  9. result <- matrix1 * matrix2
  10. cat("Result of multiplication","
  11. ")
  12. print(result)
  13.  
  14. # Divide the matrices
  15. result <- matrix1 / matrix2
  16. cat("Result of division","
  17. ")
  18. print(result)

当我们执行上面的代码,它产生以下结果 -

  1. [,1] [,2] [,3]
  2. [1,] 3 -1 2
  3. [2,] 9 4 6
  4. [,1] [,2] [,3]
  5. [1,] 5 0 3
  6. [2,] 2 9 4
  7. Result of multiplication
  8. [,1] [,2] [,3]
  9. [1,] 15 0 6
  10. [2,] 18 36 24
  11. Result of division
  12. [,1] [,2] [,3]
  13. [1,] 0.6 -Inf 0.6666667
  14. [2,] 4.5 0.4444444 1.5000000
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号