仅为偶数或奇数行将值放入矩阵

Putting values into a matrix only for even or odd numbered rows

我正在尝试专门将某些字符(字母表)插入到 R 中的矩阵中。这是我目前所拥有的。

M2 = matrix(nrow=100, ncol=26)

for (i in (1:nrow(M2)))
{
  for (j in (1:ncol(M2)))
  {
    if (i==1)
    M2[i, ] <- LETTERS
  }
  {
    if (i==2)
      M2[i, ] <- rev(LETTERS)
  }
}
M2
##        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
##   [1,] "A"  "B"  "C"  "D"  "E"  "F"  "G"  "H"  "I"  "J"   "K"   "L"   "M"  
##   [2,] "Z"  "Y"  "X"  "W"  "V"  "U"  "T"  "S"  "R"  "Q"   "P"   "O"   "N"  
##   [3,] NA   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA   
##   [4,] NA   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA   
##   [5,] NA   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA   
##   [6,] NA   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA   
##   [7,] NA   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA   
##   [8,] NA   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA   
##   [9,] NA   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA   
##  [10,] NA   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA    NA    NA   

到目前为止效果很好,但我需要每个奇数行(1、3、5 等,最多 100 个)有字母,每个偶数行最多 100 个有 rev(LETTERS) .我需要用什么来替换 if== 部分才能实现此目的?

我们可以使用 rep 复制所需的字母序列,然后使用 matrix 将它们排列成所需的结构。

my_sequence <- c(LETTERS, rev(LETTERS))
M2 <- matrix(rep(my_sequence, 50), byrow = T, nrow = 100)

       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
  [1,] "A"  "B"  "C"  "D"  "E"  "F"  "G"  "H"  "I"  "J"   "K"   "L"   "M"  
  [2,] "Z"  "Y"  "X"  "W"  "V"  "U"  "T"  "S"  "R"  "Q"   "P"   "O"   "N"  
  [3,] "A"  "B"  "C"  "D"  "E"  "F"  "G"  "H"  "I"  "J"   "K"   "L"   "M"  
  [4,] "Z"  "Y"  "X"  "W"  "V"  "U"  "T"  "S"  "R"  "Q"   "P"   "O"   "N"  
  [5,] "A"  "B"  "C"  "D"  "E"  "F"  "G"  "H"  "I"  "J"   "K"   "L"   "M"  
  [6,] "Z"  "Y"  "X"  "W"  "V"  "U"  "T"  "S"  "R"  "Q"   "P"   "O"   "N"  
... 94 more rows and 13 more columns ...

一般来说,应该利用 R 的矢量化,因为矢量通常比循环更快。但是,如果您想出于问题中未解释的原因实施 loop-based 解决方案,则可以按照以下方式进行。请注意 (i %% 2) 对于偶数行等于零,这给了我们一个很好的捷径:

  M2 <- matrix(NA, nrow=100, ncol=26)
  
  for (i in 1:nrow(M2)) {

    if (i %% 2 == 0) {
      M2[i, ] <- rev(LETTERS)
    } else {
      M2[i, ] <- LETTERS
    }
  }

或者,如果您绝对必须通过嵌套循环一次插入一个元素,您可以这样做(但我真的建议您不要这样做):

  M2 <- matrix(NA, nrow=100, ncol=26)
  
  for (i in 1:nrow(M2)) {

    if (i %% 2 == 0) {
      for (j in 1:26) {
        M2[i, j] <- rev(LETTERS)[j]
      }
    } else {
      for (j in 1:26) {
        M2[i, j] <- LETTERS[j]
      }
    }
  }

您的follow-up问题:

how would I do this procedure, but with numbers 1 to 100 alternating with numbers 100 to 1 for the columns? (nested loops required)

M2 <- matrix(NA, nrow=100, ncol=100)

for (i in 1:ncol(M2)) {

  if (i %% 2 == 0) {
    for (j in 1:100) {
      M2[j, i] <- 101 - j
    }
  } else {
    for (j in 1:100) {
      M2[j, i] <- j
    }
  }
}

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
[1,]    1  100    1  100    1  100    1  100    1   100     1   100     1   100     1   100
[2,]    2   99    2   99    2   99    2   99    2    99     2    99     2    99     2    99
[3,]    3   98    3   98    3   98    3   98    3    98     3    98     3    98     3    98
[4,]    4   97    4   97    4   97    4   97    4    97     4    97     4    97     4    97
[5,]    5   96    5   96    5   96    5   96    5    96     5    96     5    96     5    96
[6,]    6   95    6   95    6   95    6   95    6    95     6    95     6    95     6    95
... 94 more rows and 84 more columns ...

为了完整起见,loop-free 版本:

matrix(rep(c(1:100, 100:1), 50), ncol = 100)