R 从循环的迭代生成输出
所以我现在有这个脚本R 从循环的迭代生成输出,r,loops,R,Loops,所以我现在有这个脚本 for (i in 1:134) { f <- 7426+i*5 Control <- Control[ which(Control$Photo==f & Control$Distance>0 & Control$Distance<15),] attach(Control) PhysicalMode
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])) }
}
for(1:134中的i){
f您需要定义一个数据帧向量来保存迭代的输出。每次迭代后,将当前输出放入该向量(或数据帧)中
output.f
output.f <- rep(NA, 134) # define an empty vector to save the output
for (i in 1:134) {
....
output.f[i] <- f
}