Change matrix dimension

前端 未结 5 1637
离开以前
离开以前 2021-01-20 22:31

Let\'s make a replicable example:

This is my initial matrix

d<- matrix(1:80,,5)
d
     [,1] [,2] [,3] [,4] [,5]
[1,]    1   17   33   49   65
[2,         


        
5条回答
  •  离开以前
    2021-01-20 23:09

    We can try

    d1 <- array(d, c(4, 4, 5))
    do.call(rbind, lapply(seq(dim(d1)[3]), function(i) d1[,,i]))
    #       [,1] [,2] [,3] [,4]
    # [1,]    1    5    9   13
    # [2,]    2    6   10   14
    # [3,]    3    7   11   15
    # [4,]    4    8   12   16
    # [5,]   17   21   25   29
    # [6,]   18   22   26   30
    # [7,]   19   23   27   31
    # [8,]   20   24   28   32
    # [9,]   33   37   41   45
    #[10,]   34   38   42   46
    #[11,]   35   39   43   47
    #[12,]   36   40   44   48
    #[13,]   49   53   57   61
    #[14,]   50   54   58   62
    #[15,]   51   55   59   63
    #[16,]   52   56   60   64
    #[17,]   65   69   73   77
    #[18,]   66   70   74   78
    #[19,]   67   71   75   79
    #[20,]   68   72   76   80
    

提交回复
热议问题