代码之家  ›  专栏  ›  技术社区  ›  Hercules Apergis

R中的数组循环

  •  0
  • Hercules Apergis  · 技术社区  · 7 年前

    假设以下代码旨在执行以下操作: g 我想创建一个数组,其中包含从 Vijk g=1 算法运行并填充 VijkArray result.1[3,2,1:3] . 为了 g=2 同样的过程,但现在填充“result.1[3,2,4:6]等。我的问题是如何使这最后一步不发生,因为现在发生的事情是数组的前3部分不断被覆盖,我理解为什么,但未能克服它。

    g=c(0.65,0.70,0.75,0.80,0.85)
    result.1 <- array(0, dim = c(3,2,15))
    VijkArray <- array(0,dim=c(3,2,3))
    for(t in 1:length(g)){
    
      for (i in 1:3) {
        for (j in 1:3) {
          for (q in 1:2) {
            x <-xnew1[i,q,j]
            VijkArray[i,q,j]<-Vijk(x,q) #call Vijk function and update the criterion value accordingly
            result.1[i,q,j]<-VijkArray[i,q,j]
          }
        }
      }
    }
    

    Example .Rdata environment to use

    >result.1
    
    , , 1
    
    
              [,1]       [,2]
    [1,]  0.000000  0.5285917
    [2,] -1.433881 -0.5823365
    [3,] -1.866261 -1.1893313
    
    , , 2
    
               [,1]       [,2]
    [1,] -0.5823365  0.4061262
    [2,] -1.6576946 -0.9137839
    [3,] -2.0629455 -1.4338807
    
    , , 3
    
               [,1]       [,2]
    [1,] -0.9137839  0.2588162
    [2,] -1.8662608 -1.1893313
    [3,] -2.2500000 -1.6576946
    
    , , 4
    
         [,1] [,2]
    [1,]    0    0
    [2,]    0    0
    [3,]    0    0
    
    , , 5
    
         [,1] [,2]
    [1,]    0    0
    [2,]    0    0
    [3,]    0    0
    
    , , 6
    
         [,1] [,2]
    [1,]    0    0
    [2,]    0    0
    [3,]    0    0
    
    , , 7
    
         [,1] [,2]
    [1,]    0    0
    [2,]    0    0
    [3,]    0    0
    
    , , 8
    
         [,1] [,2]
    [1,]    0    0
    [2,]    0    0
    [3,]    0    0
    
    , , 9
    
         [,1] [,2]
    [1,]    0    0
    [2,]    0    0
    [3,]    0    0
    
    , , 10
    
         [,1] [,2]
    [1,]    0    0
    [2,]    0    0
    [3,]    0    0
    
    , , 11
    
         [,1] [,2]
    [1,]    0    0
    [2,]    0    0
    [3,]    0    0
    
    , , 12
    
         [,1] [,2]
    [1,]    0    0
    [2,]    0    0
    [3,]    0    0
    
    , , 13
    
         [,1] [,2]
    [1,]    0    0
    [2,]    0    0
    [3,]    0    0
    
    , , 14
    
         [,1] [,2]
    [1,]    0    0
    [2,]    0    0
    [3,]    0    0
    
    , , 15
    
         [,1] [,2]
    [1,]    0    0
    [2,]    0    0
    [3,]    0    0
    

    您将注意到,所有结果都将在数组的前3个表中报告。所以第一次 应填写表4、5和6,以此类推。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Pierre Cattin    7 年前

    更新以匹配已编辑问题的预期输出:

    g=c(0.65,0.70,0.75,0.80,0.85)
    VijkArray <- array(0,dim=c(3,2,3))
    result.1 <- array(0, dim = c(3,2,3*length(g)))
    for(t in 1:length(g)){
      for (i in 1:3) {
        for (j in 1:3) {
          for (q in 1:2) {
            x <-xnew1[i,q,j]
            VijkArray[i,q,j]<-Vijk(x,q) #call Vijk function and update the criterion value accordingly
          }
        }
      }
      indices <- ((t-1)*3+1):(t*3)
      result.1[,,indices] <- VijkArray 
    }
    

    输出:

    print(result.1)
    >, , 1
    >
    >          [,1]       [,2]
    >[1,]  0.000000  0.5285917
    >[2,] -1.433881 -0.5823365
    >[3,] -1.866261 -1.1893313
    >
    >, , 2
    >
    >           [,1]       [,2]
    >[1,] -0.5823365  0.4061262
    >[2,] -1.6576946 -0.9137839
    >[3,] -2.0629455 -1.4338807
    >
    >, , 3
    >
    >           [,1]       [,2]
    >[1,] -0.9137839  0.2588162
    >[2,] -1.8662608 -1.1893313
    >[3,] -2.2500000 -1.6576946
    >
    >, , 4
    >
    >          [,1]       [,2]
    >[1,]  0.000000  0.5032942
    >[2,] -1.433881 -0.5823365
    >[3,] -1.866261 -1.1893313
    >
    >, , 5
    >
    >           [,1]       [,2]
    >[1,] -0.5823365  0.3789291
    >[2,] -1.6576946 -0.9137839
    >[3,] -2.0629455 -1.4338807
    >
    >, , 6
    >
    >           [,1]       [,2]
    >[1,] -0.9137839  0.2332582
    >[2,] -1.8662608 -1.1893313
    >[3,] -2.2500000 -1.6576946
    >
    >, , 7
    >
    >          [,1]       [,2]
    >[1,]  0.000000  0.4792073
    >[2,] -1.433881 -0.5823365
    >[3,] -1.866261 -1.1893313
    >
    >, , 8
    >
    >           [,1]       [,2]
    >[1,] -0.5823365  0.3535534
    >[2,] -1.6576946 -0.9137839
    >[3,] -2.0629455 -1.4338807
    >
    >, , 9
    >
    >           [,1]       [,2]
    >[1,] -0.9137839  0.2102241
    >[2,] -1.8662608 -1.1893313
    >[3,] -2.2500000 -1.6576946
    >
    >, , 10
    >
    >          [,1]       [,2]
    >[1,]  0.000000  0.4562733
    >[2,] -1.433881 -0.5823365
    >[3,] -1.866261 -1.1893313
    >
    >, , 11
    >
    >           [,1]       [,2]
    >[1,] -0.5823365  0.3298770
    >[2,] -1.6576946 -0.9137839
    >[3,] -2.0629455 -1.4338807
    >
    >, , 12
    >
    >           [,1]       [,2]
    >[1,] -0.9137839  0.1894646
    >[2,] -1.8662608 -1.1893313
    >[3,] -2.2500000 -1.6576946
    >
    >, , 13
    >
    >          [,1]       [,2]
    >[1,]  0.000000  0.4344368
    >[2,] -1.433881 -0.5823365
    >[3,] -1.866261 -1.1893313
    >
    >, , 14
    >
    >           [,1]       [,2]
    >[1,] -0.5823365  0.3077861
    >[2,] -1.6576946 -0.9137839
    >[3,] -2.0629455 -1.4338807
    >
    >, , 15
    >
    >           [,1]      [,2]
    >[1,] -0.9137839  0.170755
    >[2,] -1.8662608 -1.189331
    >[3,] -2.2500000 -1.657695