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

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Oct 7 21:42:17 CEST 2005


On Fri, 7 Oct 2005, Prof Brian Ripley wrote:

> If A is the matrix the answer is 2*(1 - 2^(-A)), which took about 10secs
> for an example of your size.
>
>> From \sum_{i=1}^n x^{1-i} = (1-x^{-n})/(1-x), E & OE.

NB: I have assumed n >= 1 here, as people nornally do when using 1:count.


> On Fri, 7 Oct 2005, Tim Smith wrote:
>
>> 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?
>
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>
> ______________________________________________
> R-help at 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
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list