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

From: Berton Gunter <gunter.berton_at_gene.com>
Date: Sat 08 Oct 2005 - 05:21:01 EST


Well, since Sum(i=1 to i-n) =n*(n+1)/2, your loop simply gives 1/4*count*(count-1).
So if your matrix is A, A*(A-1)/4 is about the quickest way to get your answer I think.

"The business of the statistician is to catalyze the scientific learning process." - George E. P. Box    

> -----Original Message-----
> From: r-help-bounces@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Tim Smith
> Sent: Friday, October 07, 2005 11:48 AM
> To: r-help@stat.math.ethz.ch
> Subject: [R] Applying a function to each element of an array
>
> 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 Received on Sat Oct 08 05:26:05 2005

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