Hello Petr. For the random values, I wanted to generate a different random number for each element of my velocity matrix.
So will this do it? rmat <- matrix(runif(1000), 500,2) rmat2 <- matrix(runif(1000), 500,2) rindex <- sample(1:500, replace=TRUE) #with repetition velocity<-0.4 * velocity + rmat * (pbestsVar - popVar) + rmat2 * (archiveVar[rindex,] - popVar) Also, do the apply methods perform better than for loops given the same function? sample: apply(x, fun) and for (i in 1:length(x)) fun(x[i]) cheers cjmr -- View this message in context: http://n4.nabble.com/For-loops-in-R-tp1015933p1017196.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.