我有以下程序。我只想运行 for 循环并将每次迭代的结果保存到valg[i,]
矩阵valo[i,]
/obj[i,]
表中
我做错了,我收到以下错误
Error in `[<-`(`*tmp*`, i, , value = c(80.6413039191413, 40.0368791515846, :
subscript out of bounds
这个程序需要DEoptim包
我将不胜感激任何帮助
g = matrix(c(1,6,1,1,1/6,
1/6,1,6,1,1,
1,1/6,1,6,1,
1,1,1/6,1,6,
6,1,1,1/6,1
), nrow=5, ncol=5)
a <- t(g)
lsm <- function(x){
b <- a - outer(x,x,`/`)
z <- sum(b^2)
return(z)}
for (i in (1 : 10)){
outDEoptim <- DEoptim(lsm, lower = rep(0.001,nrow(a)),
upper = rep(100,nrow(a)),DEoptim.control(trace = FALSE))
valg[i,] = rbind(outDEoptim$member$bestmemit[200,]) ## Save for each itr i and input to optim below for each itr
lsmoptl <- optim(par=c(valg[i,]), fn=lsm, gr = NULL,
method = c("L-BFGS-B"))
valo[i,] = lsmoptl$par/sum(lsmoptl$par) ## Save for each itr i
obj[i,] = lsmoptl$value ## Save for each itr i
}