所以我目前有这个脚本
for (i in 1:134) {
f <- 7426+i*5
Control <- Control[ which(Control$Photo==f &
Control$Distance>0 &
Control$Distance<15),]
attach(Control)
PhysicalModel<-function(D, Red, Green, Blue, param){
#Left4.773540e+05
#Left7.642514e-06;
I0 <- 3.293789e+03; #5.253042e+05;
P0 <- 1.014767e-03; #3.817554e-06;
Fr <- 0.25*Red
Fg <- 0.44*Green
Fb <- 0.31*Blue;
Br1 <- abs(-3.998730e-05)*Red*abs(param[1]);
Bg1 <- abs(-2.760392e-05)*Green*abs(param[1]);
Bb1 <- abs(-3.998730e-05)*Blue*abs(param[1]);
Ar1 <- abs(2.519229e-01)*Red*abs(param[1]);
Ag1 <- abs(2.385121e-01)*Green*abs(param[1]);
Ab1 <- abs(6.000000e-01)*Blue*abs(param[1]);
I0*(Fr+Fg+Fb) *
(P0 + (Br1+Bg1+Bb1)*D)*exp(-(P0 +(Ar1+Ag1+Ab1))*D -1/2*(Br1+Bg1+Bb1)*(D^2))
}
lsfunc<-function(param,Par,Distance,Red,Green,Blue){
eta <- PhysicalModel(Distance, Red, Green, Blue, param)
sum((Par-eta)^2)
}
startparam <- c(0.1)
optval <- optim(startparam, lsfunc, NULL, method="Nelder-Mead",
control=c(maxit=100,reltol=1e-200),
Par, Distance, Red, Green, Blue)
print(optval)
print(d)
print(f)
Output <- data.frame()
for (i in 1:134) { Output <- rbind(Output,c(f[i], d[i])) }
}
因此,简而言之,我希望能够在表格或txt文件甚至数据框中同时获取f和i值,但目前脚本只是每次迭代都会更新。
答案 0 :(得分:0)
您需要定义数据帧的向量以保存迭代的输出。每次迭代后,将当前输出放入该向量(或数据帧)。
output.f <- rep(NA, 134) # define an empty vector to save the output
for (i in 1:134) {
....
output.f[i] <- f
}