
Hello

Sorry for the confusion
Here are additional details.

 velocity, popVar, archiveVar,pbestsVar are matrices with dimension of 500
row, 2 col matrix.
I want to assign new values for each velocity[i,j] using the formula below:

*0.4 * velocity[i,j] + 1 * randomvalue * (pbestsVar[i,j] - popVar[i,j]) + 1
* randomvalue * (archiveVar[random(1..500), j] -popVar[i,j]) *

with each random value different for each i j pair.

I want to do this without using the double for loops. I read somewhere that
for loops take longer to execute in R. So I want to eliminate them for
improved performance.

Long time C user and still adjusting to the R structures.

thanks for your help :)
On Mon, Jan 18, 2010 at 4:28 PM, Petr Pikal [via R] <
ml-node+1016444-458463297@n4.nabble.com<ml-node%2B1016444-458463297@n4.nabble.com>
> wrote:

> Hi
>
> Definitely not
>
> from first I get
>
> > gBest<-floor(runif(popsize,min=1,max=top))
> Error in runif(popsize, min = 1, max = top) : object 'popsize' not found
> >
>
> and from second
>
> > for (i in 1:popsize) {
> +   for (j in 1:maxvar) {
> +    gBest<-sample(top,size=1)
> +    velocity[i,j]<<-.4* velocity[i,j] + 1 * runif(1) * (pbestsVar[i,j] -
> + popVar[i,j]) + 1 * runif(1) * (archiveVar[gBest,j] - popVar[i,j])
> +    #velocity[i,j]<-.4* velocity[i,j] + 1 * .3722 * (pbestsVar[i,j] -
> + popVar[i,j]) + 1 * .3722 * (archiveVar[3,j] - popVar[i,j])
> Error: unexpected ')' in:
> "   #velocity[i,j]<-.4* velocity[i,j] + 1 * .3722 * (pbestsVar[i,j] -
> popVar[i,j])"
> >
>
> If you by chance have all data necessary for those codes not to give those
> stupid errors you could transfer their output to different objects and
> check those objects if they are equivalent.
>
> ?all.equal
>
> Regards
> Petr
>
>
> [hidden email]<http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1016444&i=0>napsal dne 17.01.2010 08:17:16:
>
> >
> > Hello.
> >
> > I've just started using R and am trying to figure out if the two codes
> > snippets below have the same output
> >
> > gBest<-floor(runif(popsize,min=1,max=top))
> > velocity[i,j]<<-.4* velocity[i,j] + 1 * runif(1) * (pbestsVar[i,j] -
> > popVar[i,j]) + 1 * runif(1) * (archiveVar[gBest,j] - popVar[i,j])
> >
> > and
> >
> > for (i in 1:popsize) {
> >       for (j in 1:maxvar) {
> >          gBest<-sample(top,size=1)
> >          velocity[i,j]<<-.4* velocity[i,j] + 1 * runif(1) *
> (pbestsVar[i,j] -
> > popVar[i,j]) + 1 * runif(1) * (archiveVar[gBest,j] - popVar[i,j])
> >          #velocity[i,j]<-.4* velocity[i,j] + 1 * .3722 * (pbestsVar[i,j]
> -
> > popVar[i,j]) + 1 * .3722 * (archiveVar[3,j] - popVar[i,j])
> >       }
> >
> > many thanks
> >
> >
> > --
> > View this message in context:
> http://n4.nabble.com/For-loops-in-R-tp1015933p1015933.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [hidden email]<http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1016444&i=1>mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [hidden email]<http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1016444&i=2>mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>
> ------------------------------
> View message @ http://n4.nabble.com/For-loops-in-R-tp1015933p1016444.html
> To unsubscribe from For loops in R, click here< (link removed) =>.
>
>
>

-- 
View this message in context: http://n4.nabble.com/For-loops-in-R-tp1015933p1016473.html
Sent from the R help mailing list archive at Nabble.com.

	[[alternative HTML version deleted]]

