Skip to content
Back to formatted view

Raw Message

Message-ID: <1263864765734-1017196.post@n4.nabble.com>
Date: 2010-01-19T01:32:45Z
From: cjmr
Subject: Odp: For loops in R
In-Reply-To: <OF220C19DA.88C0ED29-ONC12576AF.003BEE73-C12576AF.003D536C@precheza.cz>

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.