Re: [R] Applying a function to each element of an array

From: Ravi Varadhan <rvaradha_at_jhsph.edu>
Date: Sat 08 Oct 2005 - 07:05:06 EST


The sum in the loop is simply: 2 - (0.5)^count.

So you don't need this loop. As "count" gets large, the sum approaches 2.

Ravi.

> -----Original Message-----
> From: r-help-bounces@stat.math.ethz.ch [mailto:r-help-
> bounces@stat.math.ethz.ch] On Behalf Of Patrick Burns
> Sent: Friday, October 07, 2005 4:29 PM
> To: Tim Smith
> Cc: r-help@stat.math.ethz.ch
> Subject: Re: [R] Applying a function to each element of an array
>
> If there weren't an analytic solution to your problem,
> then you could build a vector of the answers from 1
> to the maximum in the matrix. Call that 'wtvec'. Then:
>
> ans <- array(NA, dim(A), dimnames(A))
> ans[] <- wtvec[as.vector(A)]
>
> should get you what you want.

>
>
> Patrick Burns
> patrick@burns-stat.com
> +44 (0)20 8525 0696
> http://www.burns-stat.com
> (home of S Poetry and "A Guide for the Unwilling S User")
>
> Tim Smith wrote:
>
> >Hi,
> >
> >I have a 7000x7000 matrix, and each element is an integer. For each
> element, I want to apply the function :
> >
> >wt <- 0
> > for(q in 1:count){
> > wt <- wt + 0.5^(q-1)
> >}
> >
> >I get the value of 'count' from the elements in the matrix , and want to
> store the corresponding 'wt' value for that element.
> >
> >I suppose I could loop through the matrix, and apply the function to each
> element but this would take a really really long time. Are there any

> quicker ways to get the same result?
> >
> >many thanks,
> >
> >Tim
> >
> >
> >---------------------------------
> >
> >
> > [[alternative HTML version deleted]]
> >
> >______________________________________________
> >R-help@stat.math.ethz.ch mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide! http://www.R-project.org/posting-
> guide.html
> >
> >
> >
> >
> >
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-
> guide.html



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Oct 08 07:12:05 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 18:29:37 EST