Hi I have a function f(k,l) which returns a matrix for integer k and l. I want to call f(.,.) for each combination of a supplied vector (as in expand.grid()) and then I want to assemble these matrices into one big one using k and l to index the position of the submatrices returned by f(k,l). Toy example follows. f <- function(k,l){ matrix(k+l , k , l) } and I want to call f() with each combination of c(1,3) for the first argument and c(2,4,5) for the second and then assemble the resulting matrices. I can do it piecemeal: a <- c(1,3) b <- c(2,4,5) expand.grid(a,b) Var1 Var2 1 1 2 2 3 2 3 1 4 4 3 4 5 1 5 6 3 5 cbind(rbind(f(1,2),f(3,2)) , rbind(f(1,4),f(3,4)) , rbind(f(1,5),f (3,5))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [1,] 3 3 5 5 5 5 6 6 6 6 6 [2,] 5 5 7 7 7 7 8 8 8 8 8 [3,] 5 5 7 7 7 7 8 8 8 8 8 [4,] 5 5 7 7 7 7 8 8 8 8 8 [see how the calls to f(. , .) follow the rows of expand.grid(a,b)] How to do this in a nice vectorized manner? -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
a crude approach is the following: f <- function(x){ k <- x[1] l <- x[2] matrix(k + l, k , l) } a <- c(1, 3) b <- c(2, 4, 5) combs <- expand.grid(a, b) do.call("cbind", lapply(split(combs, combs$Var2), function(x) do.call("rbind", apply(x, 1, f)))) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Robin Hankin" <r.hankin at noc.soton.ac.uk> To: "RHelp" <r-help at stat.math.ethz.ch> Sent: Wednesday, November 16, 2005 11:10 AM Subject: [R] matrix assembly> Hi > > I have a function f(k,l) which returns a matrix for integer k and l. > I want to call f(.,.) for each combination of a supplied vector (as > in expand.grid()) > and then I want to assemble these matrices into one big one using > k and l to index the position of the submatrices returned by f(k,l). > > Toy example follows. > > > f <- function(k,l){ > matrix(k+l , k , l) > } > > and I want to call f() with each combination of c(1,3) > for the first argument and c(2,4,5) for the second > and then assemble the resulting matrices. > > I can do it piecemeal: > > a <- c(1,3) > b <- c(2,4,5) > expand.grid(a,b) > Var1 Var2 > 1 1 2 > 2 3 2 > 3 1 4 > 4 3 4 > 5 1 5 > 6 3 5 > cbind(rbind(f(1,2),f(3,2)) , rbind(f(1,4),f(3,4)) , rbind(f(1,5),f > (3,5))) > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] > [1,] 3 3 5 5 5 5 6 6 6 6 6 > [2,] 5 5 7 7 7 7 8 8 8 8 8 > [3,] 5 5 7 7 7 7 8 8 8 8 8 > [4,] 5 5 7 7 7 7 8 8 8 8 8 > > [see how the calls to f(. , .) follow the rows of expand.grid(a,b)] > > > How to do this in a nice vectorized manner? > > > > -- > Robin Hankin > Uncertainty Analyst > National Oceanography Centre, Southampton > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > ______________________________________________ > 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 >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
On 11/16/05, Robin Hankin <r.hankin at noc.soton.ac.uk> wrote:> Hi > > I have a function f(k,l) which returns a matrix for integer k and l. > I want to call f(.,.) for each combination of a supplied vector (as > in expand.grid()) > and then I want to assemble these matrices into one big one using > k and l to index the position of the submatrices returned by f(k,l). > > Toy example follows. > > > f <- function(k,l){ > matrix(k+l , k , l) > } > > and I want to call f() with each combination of c(1,3) > for the first argument and c(2,4,5) for the second > and then assemble the resulting matrices. > > I can do it piecemeal: > > a <- c(1,3) > b <- c(2,4,5) > expand.grid(a,b) > Var1 Var2 > 1 1 2 > 2 3 2 > 3 1 4 > 4 3 4 > 5 1 5 > 6 3 5 > cbind(rbind(f(1,2),f(3,2)) , rbind(f(1,4),f(3,4)) , rbind(f(1,5),f > (3,5))) > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] > [1,] 3 3 5 5 5 5 6 6 6 6 6 > [2,] 5 5 7 7 7 7 8 8 8 8 8 > [3,] 5 5 7 7 7 7 8 8 8 8 8 > [4,] 5 5 7 7 7 7 8 8 8 8 8 > > [see how the calls to f(. , .) follow the rows of expand.grid(a,b)] > > > How to do this in a nice vectorized manner? >Replacing your example with the appropriate do.call and lapply calls gives this: do.call("cbind", lapply(b, function(x) do.call("rbind", lapply(a, f, l = x)))) or one can do the cbind in the inner loop and the rbind in the outer giving the same result: do.call("rbind", lapply(a, function(x) do.call("cbind", lapply(b, f, k = x))))