Reputation: 107
I have two lists of matrices, LIST1
(size m), and LIST2
(size n). Each matrix of LIST1
is size (p,q)
and LIST2
contains column matrices of size (p,1)
. I want to combine each of LIST2 column matrices to each LIST1. I tried lapply
, but couldn't figure that out. The size of the output list is mn
Upvotes: 1
Views: 71
Reputation: 214957
You can make a nested loop with lapply
:
unlist(lapply(LIST1, function(x) lapply(LIST2, cbind, x)), recursive = F)
Here is a small reproducible example:
LIST1 <- list(matrix(1:4, nrow = 2), matrix(2:5, nrow = 2))
LIST2 <- list(matrix(1:2, nrow = 2))
unlist(lapply(LIST1, function(x) lapply(LIST2, cbind, x)), recursive = F)
#[[1]]
# [,1] [,2] [,3]
#[1,] 1 1 3
#[2,] 2 2 4
#[[2]]
# [,1] [,2] [,3]
#[1,] 1 2 4
#[2,] 2 3 5
Upvotes: 3