This is somewhat faster on my machine:

t(sapply(seq(var1), function(i) my.list[[var1[i]]] [[var2[i]]] [var3[i], 
]))

Jean


Graaf, G de wrote on 10/14/2011 09:23:24 AM:
> 
> Hi all,
> 
> I was unable to find a solution to my problem in the archives, but 
> this might be due to a lack knowledge on the correct terminology on 
> my part. Please forgive me if this has been explained before and 
> please forgive me my probably clumsy way of explaining things.
> 
> This is what I want to do:
> 
> I have a list made up of 6 lists containing 7  4x4 matrices each.
> My goal is to select a large number of rows from all those matrices 
> (thus giving vectors of length 4), and stack them into a matrix or data 
frame.
> Creating a single such vector is easy using the normal extract 
> functionality, say I want the 2nd row of the 5th matrix in the 3rd 
> list, I use:
> my.list[[3]] [[5]] [2, ]
> 
> Problem is, I need to get a matrix or data frame of about 8000 of 
> these rows...
> 
> My idea was to create three vectors for each of the three indices, 
> and put those vector names where the 3, 5 and 2 are in my example. I
> did something similar before with a 3-dimensional array, where you 
> can use a matrix with dimensions [n,3] as an index to create a 
> vector of length n. This, however, does not work with lists. I 
> created a workaround using the lapply function (shown below) which 
> works but is incredibly slow.
> 
> extract <- function (selector) {
>         matrix.list[[selector[1]]][[selector[2]]][selector[3],]
> }
> 
> output <- function(var1,var2,var3) {
>         selector <-  as.data.frame(rbind(var1,var2,var3))
>         stack <- do.call('rbind',lapply(selector,FUN=extract))
>         stack
> }
> 
> 
> The object matix.list is the said object consisting of 6 lists 
> containing 7 4x4 matrices each. Var 1 2 and 3 are three vectors of 
> length 8000.
> 
> My problem that, although this is working, it is really slow. The 
> output is used in a monte carlo simulation with many iterations, so 
> the function is called over and over again. What parts of this code 
> are slowing it down the most, and how can I speed things up?
> 
> Thanks for all the help!
> 
> Cheers,
> Gimon

	[[alternative HTML version deleted]]

