在矩阵中创建序列

时间:2014-07-09 17:37:23

标签: r

如果向量的长度是40

,我需要矩阵中的一系列向量索引
 x <- 1:40

我期望的输出是

     1   11   21
    11   21   31
     2   12   22
    12   22   32
     3   13   23
    13   23   33
     4   14   24
    14   24   34
     5   15   25
    15   25   35
     6   16   26
    16   26   36
     7   17   27
    17   27   37
     8   18   28
    18   28   38
     9   19   29
    19   29   39
    10   20   30
    20   30   40

3 个答案:

答案 0 :(得分:2)

尝试:

 matrix(rep(1:10,each=6) +c(0,10,20,10,20,30),ncol=3,byrow=T)

答案 1 :(得分:0)

怎么样

mat <- matrix(c(0,10,20,10,20,30),nrow = 2, byrow = TRUE)

res <- lapply(1:10,function(n) mat + n)

do.call(rbind,res)

答案 2 :(得分:0)

使用您的x

matrix(mapply(c, head(x, -10), tail(x, -10)), 20, 3)

#       [,1] [,2] [,3]
#  [1,]    1   11   21
#  [2,]   11   21   31
#  [3,]    2   12   22
#  [4,]   12   22   32
#  [5,]    3   13   23
#  [6,]   13   23   33
#  [7,]    4   14   24
#  [8,]   14   24   34
#  [9,]    5   15   25
# [10,]   15   25   35
# [11,]    6   16   26
# [12,]   16   26   36
# [13,]    7   17   27
# [14,]   17   27   37
# [15,]    8   18   28
# [16,]   18   28   38
# [17,]    9   19   29
# [18,]   19   29   39
# [19,]   10   20   30
# [20,]   20   30   40