【发布时间】:2015-01-17 18:44:58
【问题描述】:
我确实有一个名为primes 的向量,长度为 100,包含前一百个素数,如下所示:
primes
[1] 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109
[30] 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271
[59] 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449
[88] 457 461 463 467 479 487 491 499 503 509 521 523 541
我还有一个名为 optm 的数据框,所有这些素数都作为变量,学习率下降作为行。例如,我通过绘制第 37 个素数得到:
plot.default(optm$step, optm$seed37, type = "l", ylab = "learning rate",
xlab = "first third iterations", main = 'seed37')
我想在 4*5 多帧上提取并保存所有这 100 个图。因此,我想要 5 个文件(= 100 个图),这就是为什么我在以下循环中实现 a 作为记录限制:
a <- 20
par(mfrow = c(4, 5))
for(i in primes) {
cln <- paste0("seed",i)
plot.default(optm$step, optm[,cln], type = "l", ylab = "learning rate", xlab = "step first third", main = cln)
for(j in 1:length(primes)) {
if (j==a){
dev.copy(png, paste0(j,'optm.png'),width=8,height=6,units="in",res=100)
a = a + 20
dev.off()
}
}
}
问题是我电脑上的记录文件相似并且具有相同的情节(第一个种子2)。我的错在哪里?
编辑:
a = 0
par(mfrow = c(4, 5))
for(i in primes) { for(j in length(primes)) {cln <- paste0("seed",i)
plot.default(optm$step, optm[,cln], type = "l", ylab = "learning rate", xlab = "step first third", main = cln)
a = (a +1)
if(a==20) {
dev.copy(png, paste0(j,'optm.png'),width=8,height=6,units="in",res=100)
dev.off()
}
}
}
这种模式适用于前 20 个。所以我编写了这样的序列:
foo = seq(0, length(primes), by=20)
现在,我需要在 if 命令中调用 foo 的任何值,而不是 20。我该如何处理?
【问题讨论】: