Skip to content
Prev 206625 / 398503 Next

Odp: For loops in R

Hi

slightly better. However it would be nice if you provided some code which 
works but not the way you want. Loops are not as problematic but often you 
achieve far better results by avoiding them and using vectorised code.

Still there are some oddities.

random values in formula below shall be the same?
random(1:500) means arbitrary value from range 1:500? If yes shall be in 
each cycle different or they could be repeated?

What I would do is

I make 500x2 random matrix

rmat <- matrix(runif(1000), 500,2)

and random index

rindex <- sample(1:500)

0.4 * velocity + rmat * (pbestsVar - popVar) + rmat * (archiveVar[rindex, 
] - popVar)

shall result in a 500x2 matrix

But I am not sure if it is what you want as you did not provide enough 
clues.

Regards
Petr

r-help-bounces at r-project.org napsal dne 18.01.2010 10:08:10:
500
below:
+ 1
that
ml-node+1016444-458463297 at n4.nabble.com<ml-node%2B1016444-458463297 at n4.nabble.com>
found
(pbestsVar[i,j] -
those
http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1016444&i=0
codes
(pbestsVar[i,j]
http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1016444&i=1
http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1016444&i=2
http://n4.nabble.com/For-loops-in-R-tp1015933p1016444.html
http://n4.nabble.com/For-loops-in-R-tp1015933p1016473.html
http://www.R-project.org/posting-guide.html