3 Apr 2011 02:14
Re: .combine in nested foreach loops !
I'm not sure exactly what you're trying to do, but I'm guessing that each
of the 3 lists that you speak of is supposed to contain two 3x3xn arrays,
where "two" is the number of lists of matrices that you're processing
("a" and "b"), and "n" is the length of "a" and "b".
If that's the case, then you can fix your code with a little bit of
post-processing to the results of the inner-foreach loop. The inner-
foreach loop is returning a list containing two lists of n matrices.
You need to convert that into a list containing two 3x3xn arrays.
A simple way to do that is with a ".final" function. The input to
the .final function is the final result from the .combine function.
The output from the .final function is returned by foreach. That is
particularly useful with nested foreach loops.
So here's what I came up with, using the abind function from
the abind package to create the 3x3xn arrays:
library(abind)
library(foreach)
library(doMC)
registerDoMC()
cvec <- c(1, 2, 3)
n <- 2
s <- seq(length=n)
a <- lapply(s, function(i) matrix(rnorm(9), 3))
b <- lapply(s, function(i) matrix(rnorm(9), 3))
init <- list(a=list(), b=list())
(Continue reading)
RSS Feed