如何按R中的行复制矩阵?
逐行复制矩阵意味着一次或多次但逐行重复矩阵。例如,如果我们有一个只包含一行和三列的矩阵,那么该矩阵的复制将被重复三遍,重复三遍。可以通过使用rep函数和矩阵函数来完成此操作,如下例所示。
示例
M<-matrix(1:25,ncol=5)M
输出结果
[,1] [,2] [,3] [,4] [,5][1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
复制矩阵M两次-
示例
matrix(rep(t(M),2),ncol=ncol(M),byrow=TRUE)
输出结果
[,1] [,2] [,3] [,4] [,5][1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
[6,] 1 6 11 16 21
[7,] 2 7 12 17 22
[8,] 3 8 13 18 23
[9,] 4 9 14 19 24
[10,] 5 10 15 20 25
复制矩阵M三次-
示例
matrix(rep(t(M),3),ncol=ncol(M),byrow=TRUE)
输出结果
[,1] [,2] [,3] [,4] [,5][1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
[6,] 1 6 11 16 21
[7,] 2 7 12 17 22
[8,] 3 8 13 18 23
[9,] 4 9 14 19 24
[10,] 5 10 15 20 25
[11,] 1 6 11 16 21
[12,] 2 7 12 17 22
[13,] 3 8 13 18 23
[14,] 4 9 14 19 24
[15,] 5 10 15 20 25
五次复制矩阵M-
示例
matrix(rep(t(M),5),ncol=ncol(M),byrow=TRUE)
输出结果
[,1] [,2] [,3] [,4] [,5][1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
[6,] 1 6 11 16 21
[7,] 2 7 12 17 22
[8,] 3 8 13 18 23
[9,] 4 9 14 19 24
[10,] 5 10 15 20 25
[11,] 1 6 11 16 21
[12,] 2 7 12 17 22
[13,] 3 8 13 18 23
[14,] 4 9 14 19 24
[15,] 5 10 15 20 25
[16,] 1 6 11 16 21
[17,] 2 7 12 17 22
[18,] 3 8 13 18 23
[19,] 4 9 14 19 24
[20,] 5 10 15 20 25
[21,] 1 6 11 16 21
[22,] 2 7 12 17 22
[23,] 3 8 13 18 23
[24,] 4 9 14 19 24
[25,] 5 10 15 20 25
五次复制矩阵M-
示例
matrix(rep(t(M),6),ncol=ncol(M),byrow=TRUE)
输出结果
[,1] [,2] [,3] [,4] [,5][1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
[6,] 1 6 11 16 21
[7,] 2 7 12 17 22
[8,] 3 8 13 18 23
[9,] 4 9 14 19 24
[10,] 5 10 15 20 25
[11,] 1 6 11 16 21
[12,] 2 7 12 17 22
[13,] 3 8 13 18 23
[14,] 4 9 14 19 24
[15,] 5 10 15 20 25
[16,] 1 6 11 16 21
[17,] 2 7 12 17 22
[18,] 3 8 13 18 23
[19,] 4 9 14 19 24
[20,] 5 10 15 20 25
[21,] 1 6 11 16 21
[22,] 2 7 12 17 22
[23,] 3 8 13 18 23
[24,] 4 9 14 19 24
[25,] 5 10 15 20 25
[26,] 1 6 11 16 21
[27,] 2 7 12 17 22
[28,] 3 8 13 18 23
[29,] 4 9 14 19 24
[30,] 5 10 15 20 25
以上是 如何按R中的行复制矩阵? 的全部内容, 来源链接: utcz.com/z/357668.html