在 R 中将矩阵转换为向量
在本文中,我们将使用 R 编程语言将给定的矩阵转换为向量。
按行将矩阵转换为向量
方法一:使用c()函数
只需传递矩阵的名称即可完成这项工作。
句法:
c(matrix_name)
Where matrix_name is the name of the input matrix
示例 1:
R
# create a matrix with 12 elements
# with 4 rows and 3 columns
matrix=matrix(1:12,nrow=4,ncol=3)
print(matrix)
# convert matrix to vector using c()
# function
a=c(matrix)
print(a)
R
# create a matrix with 16 elements
# with 4 rows and 4 columns
matrix=matrix(1:16,nrow=4,ncol=4)
print(matrix)
# convert matrix to vector using
# c() function
a=c(matrix)
print(a)
R
# create a matrix with 12 elements
# with 4 rows and 3 columns
matrix=matrix(1:12,nrow=4,ncol=3)
print(matrix)
# convert matrix to vector using
# as.vector() function
a=as.vector(matrix)
print(a)
R
# create a matrix with 16 elements with 4 rows and 4 columns
matrix=matrix(1:16,nrow=4,ncol=4)
print(matrix)
# convert matrix to vector using as.vector() function
a=as.vector(matrix)
print(a)
R
# create a matrix with 12 elements
# with 4 rows and 3 columns
matrix=matrix(1:12,nrow=4,ncol=3)
print(matrix)
# convert matrix to vector using
# c() function along with t()
a=c(t(matrix))
print(a)
R
# create a matrix with 12 elements
# with 2 rows and 6 columns
matrix=matrix(1:12,nrow=2,ncol=6)
print(matrix)
# convert matrix to vector using
# c() function along with t()
a=c(t(matrix))
print(a)
R
# create a matrix with 12 elements
# with 2 rows and 6 columns
matrix=matrix(1:12,nrow=2,ncol=6)
print(matrix)
# convert matrix to vector using
# as.vector() function along with t()
a=as.vector(t(matrix))
print(a)
R
# create a matrix with 4 elements
# with 2 rows and 2 columns
matrix=matrix(1:4,nrow=2,ncol=2)
print(matrix)
# convert matrix to vector using
# as.vector() function along with t()
a=as.vector(t(matrix))
print(a)
输出:
示例 2:
电阻
# create a matrix with 16 elements
# with 4 rows and 4 columns
matrix=matrix(1:16,nrow=4,ncol=4)
print(matrix)
# convert matrix to vector using
# c() function
a=c(matrix)
print(a)
输出:
方法二:使用 as.vector()函数
此函数用于将矩阵转换为向量,因此再次简单地传递矩阵名称就足够了。
句法:
as.vector(matrix)
例子:
电阻
# create a matrix with 12 elements
# with 4 rows and 3 columns
matrix=matrix(1:12,nrow=4,ncol=3)
print(matrix)
# convert matrix to vector using
# as.vector() function
a=as.vector(matrix)
print(a)
输出:
示例 2:
电阻
# create a matrix with 16 elements with 4 rows and 4 columns
matrix=matrix(1:16,nrow=4,ncol=4)
print(matrix)
# convert matrix to vector using as.vector() function
a=as.vector(matrix)
print(a)
输出
按列将矩阵转换为向量
方法 1:使用 c()函数和 t()函数
t()函数用于转置给定的矩阵。它会将行转置为列,将列转置为行。
语法:
t(matrix)
where the matrix is the input matrix
应用 t() 后,我们可以应用 c() 和 as.vector() 函数将矩阵转换为向量
句法:
c(t(matrix))
示例 1:
电阻
# create a matrix with 12 elements
# with 4 rows and 3 columns
matrix=matrix(1:12,nrow=4,ncol=3)
print(matrix)
# convert matrix to vector using
# c() function along with t()
a=c(t(matrix))
print(a)
输出:
示例 2:
电阻
# create a matrix with 12 elements
# with 2 rows and 6 columns
matrix=matrix(1:12,nrow=2,ncol=6)
print(matrix)
# convert matrix to vector using
# c() function along with t()
a=c(t(matrix))
print(a)
输出:
方法 2:使用 as.vector()函数和 t()函数
t() 的工作与上面相同。进行转置后,使用 as.vector() 将矩阵转换为向量。
句法:
as.vector(t(matrix))
例子
电阻
# create a matrix with 12 elements
# with 2 rows and 6 columns
matrix=matrix(1:12,nrow=2,ncol=6)
print(matrix)
# convert matrix to vector using
# as.vector() function along with t()
a=as.vector(t(matrix))
print(a)
输出:
示例 2:
电阻
# create a matrix with 4 elements
# with 2 rows and 2 columns
matrix=matrix(1:4,nrow=2,ncol=2)
print(matrix)
# convert matrix to vector using
# as.vector() function along with t()
a=as.vector(t(matrix))
print(a)
输出