So, you can use replicate
instead of a for
loop, wrap it in a function, and then you're done.
time<-c(65,130,195,260)
initial<-.5
# I assume a,b,c,j are constants
a<-b<-c<-j<-1
# Use replicate instead of a four loop to generate your matrix directly:
mat<-replicate(10,(initial*exp(-a*(time/260)) + b*(1-exp(-a*(time/260))) + c*(j/260))*rnorm(1))
# Get the means for all four variables (note matrix is transposed)
rowMeans(mat)
# Wrap this in a function:
f<-function(initial) {
mat<-replicate(10,(initial*exp(-a*(time/260)) + b*(1-exp(-a*(time/260))) + c*(j/260))*rnorm(1))
rowMeans(mat)
}
# Now repeat for a variety of "initial"
sapply(seq(from=0, to=10, by=.5),f)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
# [1,] 0.04131179 -0.1703167 0.2165282 0.4518704 -0.4305232 1.0390553 -0.09262142 -0.3767832
# [2,] 0.07293559 -0.1941923 0.2165282 0.4239343 -0.3889186 0.9154405 -0.08016291 -0.3217918
# [3,] 0.09756422 -0.2127866 0.2165282 0.4021776 -0.3565170 0.8191691 -0.07046022 -0.2789645
# [4,] 0.11674502 -0.2272679 0.2165282 0.3852335 -0.3312825 0.7441930 -0.06290376 -0.2456105