# R矩阵用法详解

## 本文概述

### 例子

``````matrix1<-matrix(c(11, 13, 15, 12, 14, 16), nrow =2, ncol =3, byrow = TRUE)
matrix1``````

``````[, 1]  [, 2]  [, 3]
[1, ]   11   13   15
[2, ]   12   14   16``````

## R中的矩阵历史

“矩阵”一词是子宫的拉丁词, 意指形成或生产某种东西的地方。两位具有历史重要性的作者以特殊方式使用”矩阵”一词。他们提出了这个公理, 作为将任何函数简化为较低类型之一的一种手段, 以便在”底部”(0阶)处, 该函数与其扩展名相同。

## 如何在R中创建矩阵？

``matrix(data, nrow, ncol, byrow, dim_name)``

row

byrow参数是一个逻辑线索。如果其值为true, 则按行排列输入向量元素。

dim_name

dim_name参数是分配给行和列的名称。

``````#Arranging elements sequentially by row.
P <- matrix(c(5:16), nrow = 4, byrow = TRUE)
print(P)

# Arranging elements sequentially by column.
Q <- matrix(c(3:14), nrow = 4, byrow = FALSE)
print(Q)

# Defining the column and row names.
row_names = c("row1", "row2", "row3", "row4")
col_names = c("col1", "col2", "col3")

R <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))
print(R)``````

``````[, 1] [, 2] [, 3]
[1, ]    5    6    7
[2, ]    8    9   10
[3, ]   11   12   13
[4, ]   14   15   16

[, 1] [, 2] [, 3]
[1, ]    3    7   11
[2, ]    4    8   12
[3, ]    5    9   13
[4, ]    6   10   14

col1 col2 col3
row1    3    4    5
row2    6    7    8
row3    9   10   11
row4   12   13   14``````

## 在R中访问矩阵元素

1. 我们可以访问第n行和第m列上显示的元素。
2. 我们可以访问第n行上存在的矩阵的所有元素。
3. 我们还可以访问第m列上存在的矩阵的所有元素。

``````# Defining the column and row names.
row_names = c("row1", "row2", "row3", "row4")
col_names = c("col1", "col2", "col3")
#Creating matrix
R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))
print(R)

#Accessing element present on 3rd row and 2nd column
print(R[3, 2])

#Accessing element present in 3rd row
print(R[3, ])

#Accessing element present in 2nd column
print(R[, 2])``````

``````col1 col2 col3
row1    5    6    7
row2    8    9   10
row3   11   12   13
row4   14   15   16

[1] 12

col1 col2 col3
11   12   13

row1 row2 row3 row4
6    9   12   15``````

## 矩阵修改

R允许我们在矩阵中进行修改。有几种方法可以在矩阵中进行修改, 如下所示：

### 分配一个元素

``matrix[n, m]<-y``

``````# Defining the column and row names.
row_names = c("row1", "row2", "row3", "row4")
col_names = c("col1", "col2", "col3")

R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))
print(R)

#Assigning value 20 to the element at 3d roe and 2nd column
R[3, 2]<-20
print(R)``````

``````col1 col2 col3
row1    5    6    7
row2    8    9   10
row3   11   12   13
row4   14   15   16

col1 col2 col3
row1    5    6    7
row2    8    9   10
row3   11   20   13
row4   14   15   16``````

### 关系运算符的使用

R提供了执行基质药物治疗的另一种方法。在这种方法中, 我们使用了一些关系运算符, 例如>, <, ==。与第一种方法一样, 第二种方法非常易于使用。让我们看一个示例, 以了解此方法如何修改矩阵。

``````# Defining the column and row names.
row_names = c("row1", "row2", "row3", "row4")
col_names = c("col1", "col2", "col3")

R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))
print(R)

#Replacing element that equal to the 12
R[R==12]<-0
print(R)``````

``````col1 col2 col3
row1    5    6    7
row2    8    9   10
row3   11   12   13
row4   14   15   16

col1 col2 col3
row1    5    6    7
row2    8    9   10
row3   11    0   13
row4   14   15   16``````

``````# Defining the column and row names.
row_names = c("row1", "row2", "row3", "row4")
col_names = c("col1", "col2", "col3")

R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))
print(R)

#Replacing elements whose values are greater than 12
R[R>12]<-0
print(R)``````

``````col1 col2 col3
row1    5    6    7
row2    8    9   10
row3   11   12   13
row4   14   15   16

col1 col2 col3
row1    5    6    7
row2    8    9   10
row3   11   12    0
row4    0    0    0``````

### 行和列的加法

``````# Defining the column and row names.
row_names = c("row1", "row2", "row3", "row4")
col_names = c("col1", "col2", "col3")

R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))
print(R)

rbind(R, c(17, 18, 19))

cbind(R, c(17, 18, 19, 20))

#transpose of the matrix using the t() function:
t(R)

#Modifying the dimension of the matrix using the dim() function
dim(R)<-c(1, 12)
print(R)``````

``````col1 col2 col3
row1    5    6    7
row2    8    9   10
row3   11   12   13
row4   14   15   16

col1 col2 col3
row1    5    6    7
row2    8    9   10
row3   11   12   13
row4   14   15   16
17   18   19

col1 col2 col3
row1    5    6    7 17
row2    8    9   10 18
row3   11   12   13 19
row4   14   15   16 20

row1 row2 row3 row4
col1    5    8   11   14
col2    6    9   12   15
col3    7   10   13   16

[, 1] [, 2] [, 3] [, 4] [, 5] [, 6] [, 7] [, 8] [, 9] [, 10] [, 11] [, 12]
[1, ]    5    8   11   14    6    9   12   15    7    10    13    16``````

## 矩阵运算

``````R <- matrix(c(5:16), nrow = 4, ncol=3)
S <- matrix(c(1:12), nrow = 4, ncol=3)

sum<-R+S
print(sum)

#Subtraction
sub<-R-S
print(sub)

#Multiplication
mul<-R*S
print(mul)

#Multiplication by constant
mul1<-R*12
print(mul1)

#Division
div<-R/S
print(div)``````

``````[, 1] [, 2] [, 3]
[1, ]    6   14   22
[2, ]    8   16   24
[3, ]   10   18   26
[4, ]   12   20   28

[, 1] [, 2] [, 3]
[1, ]    4    4    4
[2, ]    4    4    4
[3, ]    4    4    4
[4, ]    4    4    4

[, 1] [, 2] [, 3]
[1, ]    5   45  117
[2, ]   12   60  140
[3, ]   21   77  165
[4, ]   32   96  192

[, 1] [, 2] [, 3]
[1, ]   60  108  156
[2, ]   72  120  168
[3, ]   84  132  180
[4, ]   96  144  192

[, 1]     [, 2]      [, 3]
[1, ] 5.000000 1.800000 1.444444
[2, ] 3.000000 1.666667 1.400000
[3, ] 2.333333 1.571429 1.363636
[4, ] 2.000000 1.500000 1.333333``````

## 矩阵的应用

1. 在地质学中, 矩阵负责调查和绘制图形, 统计数据, 并用于不同领域的研究。
2. 矩阵是一种表示方法, 可帮助绘制常见的调查对象。
3. 在机器人技术和自动化领域, 矩阵是机器人运动的最重要要素。
4. 矩阵在经济学中主要用于计算国内生产总值, 也有助于计算商品和产品的能力。
5. 在基于计算机的应用程序中, 矩阵在创建逼真的外观运动中起着至关重要的作用。

• 回顶