(Strictly answering the question, not pointing you to a better approach for you particular use here....)
mapply
is the function from the *apply
family of functions for applying a function while looping through multiple arguments.
So what you want to do here is turn each of your matrices into a list of vectors that hold its rows or columns (you did not specify). There are many ways to do that, I like to use the following function:
split.array.along <- function(X, MARGIN) {
require(abind)
lapply(seq_len(dim(X)[MARGIN]), asub, x = X, dims = MARGIN)
}
Then all you have to do is run:
mapply(foo, split.array.along(a, 1),
split.array.along(b, 1))
Like sapply
, mapply
tries to put your output into an array if possible. If instead you prefer the output to be a list, add SIMPLIFY = FALSE
to the mapply
call, or equivalently, use the Map function:
Map(foo, split.array.along(a, 1),
split.array.along(b, 1))