📜  R – 矩阵

📅  最后修改于: 2022-05-13 01:54:43.294000             🧑  作者: Mango

R – 矩阵

矩阵是行和列中数字的矩形排列。在矩阵中,我们知道行是水平运行的,列是垂直运行的。在 R 编程中,矩阵是二维的同质数据结构。以下是一些矩阵示例:

创建矩阵

要在 R 中创建矩阵,您需要使用名为matrix()的函数。这个matrix()的参数是向量中的元素集。您必须传递矩阵中要包含的行数和列数。

R
# R program to create a matrix
  
A = matrix(
     
  # Taking sequence of elements 
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
   
  # No of rows
  nrow = 3,  
   
  # No of columns
  ncol = 3,        
   
  # By default matrices are in column-wise order
  # So this parameter decides how to arrange the matrix
  byrow = TRUE         
)
  
# Naming rows
rownames(A) = c("a", "b", "c")
  
# Naming columns
colnames(A) = c("c", "d", "e")
  
cat("The 3x3 matrix:\n")
print(A)


R
# R program to illustrate
# special matrices
 
# Matrix having 3 rows and 3 columns
# filled by a single constant 5
print(matrix(5, 3, 3))


R
# R program to illustrate
# special matrices
 
# Diagonal matrix having 3 rows and 3 columns
# filled by array of elements (5, 3, 3)
print(diag(c(5, 3, 3), 3, 3))


R
# R program to illustrate
# special matrices
 
# Identity matrix having
# 3 rows and 3 columns
print(diag(1, 3, 3))


R
# R program to illustrate
# matrix metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
cat("Dimension of the matrix:\n")
print(dim(A))
 
cat("Number of rows:\n")
print(nrow(A))
 
cat("Number of columns:\n")
print(ncol(A))
 
cat("Number of elements:\n")
print(length(A))
# OR
print(prod(dim(A)))


R
# R program to illustrate
# access rows in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
# Accessing first and second row
cat("Accessing first and second row\n")
print(A[1:2, ])


R
# R program to illustrate
# access columns in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
# Accessing first and second column
cat("Accessing first and second column\n")
print(A[, 1:2])


R
# R program to illustrate
# access an entry in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
# Accessing 2
print(A[1, 2])
 
# Accessing 6
print(A[2, 3])


R
# R program to illustrate
# access submatrices in a matrix
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
cat("Accessing the first three rows and the first two columns\n")
print(A[1:3, 1:2])


R
# R program to illustrate
# editing elements in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
# Editing the 3rd rows and 3rd column element
# from 9 to 30
# by direct assignments
A[3, 3] = 30
 
cat("After edited the matrix\n")
print(A)


R
# R program to illustrate
# concatenation of a row in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
# Creating another 1x3 matrix
B = matrix(
  c(10, 11, 12),
  nrow = 1,
  ncol = 3
)
cat("The 1x3 matrix:\n")
print(B)
 
# Add a new row using rbind()
C = rbind(A, B)
 
cat("After concatenation of a row:\n")
print(C)


R
# R program to illustrate
# concatenation of a column in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
# Creating another 3x1 matrix
B = matrix(
  c(10, 11, 12),
  nrow = 3,
  ncol = 1,
  byrow = TRUE
)
cat("The 3x1 matrix:\n")
print(B)
 
# Add a new column using cbind()
C = cbind(A, B)
 
cat("After concatenation of a column:\n")
print(C)


R
# R program to illustrate
# Dimension inconsistency in metrics concatenation
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
# Creating another 1x3 matrix
B = matrix(
  c(10, 11, 12),
  nrow = 1,
  ncol = 3,
)
cat("The 1x3 matrix:\n")
print(B)
 
# This will give an error
# because of dimension inconsistency
C = cbind(A, B)
 
cat("After concatenation of a column:\n")
print(C)


R
# R program to illustrate
# row deletion in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("Before deleting the 2nd row\n")
print(A)
 
# 2nd-row deletion
A = A[-2, ]
 
cat("After deleted the 2nd row\n")
print(A)


R
# R program to illustrate
# column deletion in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("Before deleting the 2nd column\n")
print(A)
 
# 2nd-row deletion
A = A[, -2]
 
cat("After deleted the 2nd column\n")
print(A)


输出:

The 3x3 matrix:
  c d e
a 1 2 3
b 4 5 6
c 7 8 9

创建特殊矩阵

R 允许使用传递给 matrix()函数的参数创建各种不同类型的矩阵。

  • 所有行和列都由单个常量“k”填充的矩阵:
    要创建这样的矩阵,语法如下:
  • 例子:

R

# R program to illustrate
# special matrices
 
# Matrix having 3 rows and 3 columns
# filled by a single constant 5
print(matrix(5, 3, 3))
  • 输出:
[,1] [,2] [,3]
[1,]    5    5    5
[2,]    5    5    5
[3,]    5    5    5
  • 对角矩阵:
    对角矩阵是主对角线以外的元素全部为零的矩阵。要创建这样的矩阵,语法如下:
  • 例子:

R

# R program to illustrate
# special matrices
 
# Diagonal matrix having 3 rows and 3 columns
# filled by array of elements (5, 3, 3)
print(diag(c(5, 3, 3), 3, 3))
  • 输出:
[,1] [,2] [,3]
[1,]    5    0    0
[2,]    0    3    0
[3,]    0    0    3
  • 单位矩阵:
    一个方阵,其中主对角线的所有元素都是 1,所有其他元素都是 0。要创建这样的矩阵,语法如下:
  • 例子:

R

# R program to illustrate
# special matrices
 
# Identity matrix having
# 3 rows and 3 columns
print(diag(1, 3, 3))
  • 输出:
[,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1

矩阵度量

矩阵度量意味着一旦创建了矩阵,那么

  • 你怎么知道矩阵的维数?
  • 你怎么知道矩阵中有多少行?
  • 矩阵中有多少列?
  • 矩阵中有多少元素?是我们通常想要回答的问题。

例子:

R

# R program to illustrate
# matrix metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
cat("Dimension of the matrix:\n")
print(dim(A))
 
cat("Number of rows:\n")
print(nrow(A))
 
cat("Number of columns:\n")
print(ncol(A))
 
cat("Number of elements:\n")
print(length(A))
# OR
print(prod(dim(A)))

输出:

The 3x3 matrix:
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
Dimension of the matrix:
[1] 3 3
Number of rows:
[1] 3
Number of columns:
[1] 3
Number of elements:
[1] 9
[1] 9

访问矩阵的元素

我们可以使用数据帧中遵循的相同约定来访问矩阵中的元素。因此,您将有一个矩阵,后跟一个方括号,数组之间有一个逗号。逗号之前的值用于访问行,逗号之后的值用于访问列。让我们通过一个简单的 R 代码来说明这一点。
访问行:

R

# R program to illustrate
# access rows in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
# Accessing first and second row
cat("Accessing first and second row\n")
print(A[1:2, ])

输出:

The 3x3 matrix:
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    4    5    6
[3, ]    7    8    9

Accessing first and second row
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    4    5    6

访问列:

R

# R program to illustrate
# access columns in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
# Accessing first and second column
cat("Accessing first and second column\n")
print(A[, 1:2])

输出:

The 3x3 matrix:
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    4    5    6
[3, ]    7    8    9

Accessing first and second column
     [, 1] [, 2]
[1, ]    1    2
[2, ]    4    5
[3, ]    7    8

访问矩阵的元素:

R

# R program to illustrate
# access an entry in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
# Accessing 2
print(A[1, 2])
 
# Accessing 6
print(A[2, 3])

输出:

The 3x3 matrix:
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    4    5    6
[3, ]    7    8    9

[1] 2
[1] 6

访问子矩阵:
我们可以使用冒号(:)运算符访问矩阵中的子矩阵。

R

# R program to illustrate
# access submatrices in a matrix
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
cat("Accessing the first three rows and the first two columns\n")
print(A[1:3, 1:2])

输出:

The 3x3 matrix:
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    4    5    6
[3, ]    7    8    9

Accessing the first three rows and the first two columns
     [, 1] [, 2]
[1, ]    1    2
[2, ]    4    5
[3, ]    7    8

修改矩阵的元素

在 R 中,您可以通过直接赋值来修改矩阵的元素。
例子:

R

# R program to illustrate
# editing elements in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
# Editing the 3rd rows and 3rd column element
# from 9 to 30
# by direct assignments
A[3, 3] = 30
 
cat("After edited the matrix\n")
print(A)

输出:

The 3x3 matrix:
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    4    5    6
[3, ]    7    8    9

After edited the matrix
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    4    5    6
[3, ]    7    8   30

矩阵连接

矩阵连接是指合并现有矩阵的行或列。
连接一行:
使用rbind()将行连接到矩阵。

R

# R program to illustrate
# concatenation of a row in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
# Creating another 1x3 matrix
B = matrix(
  c(10, 11, 12),
  nrow = 1,
  ncol = 3
)
cat("The 1x3 matrix:\n")
print(B)
 
# Add a new row using rbind()
C = rbind(A, B)
 
cat("After concatenation of a row:\n")
print(C)

输出:

The 3x3 matrix:
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    4    5    6
[3, ]    7    8    9

The 1x3 matrix:
     [, 1] [, 2] [, 3]
[1, ]   10   11   12

After concatenation of a row:
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    4    5    6
[3, ]    7    8    9
[4, ]   10   11   12

列的连接:
使用cbind()将列连接到矩阵。

R

# R program to illustrate
# concatenation of a column in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
# Creating another 3x1 matrix
B = matrix(
  c(10, 11, 12),
  nrow = 3,
  ncol = 1,
  byrow = TRUE
)
cat("The 3x1 matrix:\n")
print(B)
 
# Add a new column using cbind()
C = cbind(A, B)
 
cat("After concatenation of a column:\n")
print(C)

输出:

The 3x3 matrix:
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    4    5    6
[3, ]    7    8    9

The 3x1 matrix:
     [, 1]
[1, ]   10
[2, ]   11
[3, ]   12

After concatenation of a column:
     [, 1] [, 2] [, 3] [, 4]
[1, ]    1    2    3   10
[2, ]    4    5    6   11
[3, ]    7    8    9   12

维度不一致:请注意,在执行此矩阵连接之前,您必须确保矩阵之间维度的一致性。

R

# R program to illustrate
# Dimension inconsistency in metrics concatenation
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("The 3x3 matrix:\n")
print(A)
 
# Creating another 1x3 matrix
B = matrix(
  c(10, 11, 12),
  nrow = 1,
  ncol = 3,
)
cat("The 1x3 matrix:\n")
print(B)
 
# This will give an error
# because of dimension inconsistency
C = cbind(A, B)
 
cat("After concatenation of a column:\n")
print(C)

输出:

The 3x3 matrix:
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    4    5    6
[3, ]    7    8    9

The 1x3 matrix:
     [, 1] [, 2] [, 3]
[1, ]   10   11   12

Error in cbind(A, B) : number of rows of matrices must match (see arg 2)

删除矩阵的行和列

要删除行或列,首先,您需要访问该行或列,然后在该行或列之前插入一个负号。它表明您必须删除该行或列。
行删除:

R

# R program to illustrate
# row deletion in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("Before deleting the 2nd row\n")
print(A)
 
# 2nd-row deletion
A = A[-2, ]
 
cat("After deleted the 2nd row\n")
print(A)

输出:

Before deleting the 2nd row
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    4    5    6
[3, ]    7    8    9

After deleted the 2nd row
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    7    8    9

列删除:

R

# R program to illustrate
# column deletion in metrics
 
# Create a 3x3 matrix
A = matrix(
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
  nrow = 3,            
  ncol = 3,            
  byrow = TRUE         
)
cat("Before deleting the 2nd column\n")
print(A)
 
# 2nd-row deletion
A = A[, -2]
 
cat("After deleted the 2nd column\n")
print(A)

输出:

Before deleting the 2nd column
     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    4    5    6
[3, ]    7    8    9

After deleted the 2nd column
     [, 1] [, 2]
[1, ]    1    3
[2, ]    4    6
[3, ]    7    9