I just tried your problem in R (Windows version 1.8.1):
> set.seed(2)
> jja <- array(rnorm(3000*300*3), c(3000,300,3))
> system.time(jjsa <- apply(jja, 1:2, sum))
[1] 75.21 0.45 99.43 NA NA
> dim(jjsa)
[1] 3000 300
So the solution arrives in 100 seconds on a P3 744 MHz laptop
with 256M of memory.
Patrick Burns
Burns Statistics
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")
David L Lorenz wrote:
Hi,
I ran into an interesting question from one of our users. He had an array
of about 3000 by 300 by 3. He tried to use apply to sum the last dimension:
result <- apply(array, c(1,2), sum)
I'm not sure he was ever able to get the result. He was surprised
because he could use apply over different dimensions and had no problem:
wrong.result <- apply(array, c(2,3), sum)
I suggested that he simply break down the problem into a simple
summation:
result <- array[,,1] + array[,,2] + array[,,3]
That executed very fast.
My question is "Has anybody constructed a list of functions that do not
scale well under certain circumstances?" I remember seeing something
within the last year about outer being very slow for long vectors and
clearly, there are some problems with apply.
Thanks.
Dave
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu. To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message: unsubscribe s-news
|