How to make
The first has 3 elements. Each element is 5*4 matrix. Let's call these elements m1
, m2
, m3
The second has 3 elements too. Each element is 5*4 matrix. Let's call these elements n1
, n2
, n3
set.seed(1001)
first<-replicate(3,list(matrix(unlist(replicate(5,sample(c(1,2,3,4),4,replace=TRUE))),ncol=4)))
second<-replicate(3,list(matrix(unlist(replicate(5,sample(c(5,6,7,8),4,replace=TRUE))),ncol=4)))
> first
[[1]]
[,1] [,2] [,3] [,4]
[1,] 4 4 2 4
[2,] 2 1 1 1
[3,] 2 1 4 2
[4,] 2 2 2 1
[5,] 2 4 2 4
[[2]]
[,1] [,2] [,3] [,4]
[1,] 3 4 3 1
[2,] 2 3 4 1
[3,] 4 2 4 3
[4,] 3 4 2 3
[5,] 2 2 1 1
[[3]]
[,1] [,2] [,3] [,4]
[1,] 2 1 2 3
[2,] 1 4 2 2
[3,] 3 1 1 1
[4,] 1 3 3 1
[5,] 4 3 3 4
> second
[[1]]
[,1] [,2] [,3] [,4]
[1,] 7 7 8 5
[2,] 7 8 8 5
[3,] 7 5 8 7
[4,] 6 5 8 7
[5,] 6 6 6 5
[[2]]
[,1] [,2] [,3] [,4]
[1,] 6 7 8 8
[2,] 8 5 7 7
[3,] 7 6 5 8
[4,] 6 6 5 8
[5,] 7 6 7 5
[[3]]
[,1] [,2] [,3] [,4]
[1,] 5 7 7 6
[2,] 6 8 6 8
[3,] 5 5 5 5
[4,] 7 7 7 6
[5,] 5 8 8 5
What I want is cbind each element in first
with each column of each element in second
.
n1
has 4 columns. Let's call each column n1c1
, n1c2
, n1c3
, n1c4
.
So, I want to generate m1
+n1c1
, m1
+n1c2
, m1
+n1c3
, m1
+n1c4
.
Other things go on like this.
m2
+n2c1
, m2
+n2c2
, m2
+n2c3
, m2
+n2c4
m3
+n3c1
, m3
+n3c2
, m3
+n3c3
, m3
+n3c4
These will be like this.
[[1]]
[,1] [,2] [,3] [,4] [,5]
[1,] 4 4 2 4 7
[2,] 2 1 1 1 7
[3,] 2 1 4 2 7
[4,] 2 2 2 1 6
[5,] 2 4 2 4 6
[[2]]
[,1] [,2] [,3] [,4] [,5]
[1,] 4 4 2 4 7
[2,] 2 1 1 1 8
[3,] 2 1 4 2 5
[4,] 2 2 2 1 5
[5,] 2 4 2 4 6
[[3]]
[,1] [,2] [,3] [,4] [,5]
[1,] 4 4 2 4 8
[2,] 2 1 1 1 8
[3,] 2 1 4 2 8
[4,] 2 2 2 1 8
[5,] 2 4 2 4 6
...
[[12]]
[,1] [,2] [,3] [,4] [,5]
[1,] 2 1 2 3 6
[2,] 1 4 2 2 8
[3,] 3 1 1 1 5
[4,] 1 3 3 1 6
[5,] 4 3 3 4 5
I tried these code. But it all failed.
out<-lapply(first, function(x) (cbind(first, second=x)))
out
[[1]]
first
[1,] Numeric,20 1 1 4 2
[2,] Numeric,20 2 2 1 4
[3,] Numeric,20 4 1 4 3
[4,] Numeric,20 4 4 2 1
[5,] Numeric,20 2 3 4 4
[[2]]
first
[1,] Numeric,20 1 3 1 4
[2,] Numeric,20 3 1 3 2
[3,] Numeric,20 2 2 4 1
[4,] Numeric,20 4 4 3 3
[5,] Numeric,20 2 4 1 3
[[3]]
first
[1,] Numeric,20 3 1 4 1
[2,] Numeric,20 4 2 2 1
[3,] Numeric,20 3 2 1 1
[4,] Numeric,20 2 2 3 1
[5,] Numeric,20 2 1 4 4
(I didn't even know what is 'Numeric,20'!)
And this even failed.
out<-lapply(first, function(x) lapply(1:nrow(x), function(y) cbind(first, second=y)))
out
[[1]]
[[1]][[1]]
first second
[1,] Numeric,20 1
[2,] Numeric,20 1
[3,] Numeric,20 1
[[1]][[2]]
first second
[1,] Numeric,20 2
[2,] Numeric,20 2
[3,] Numeric,20 2
[[1]][[3]]
first second
[1,] Numeric,20 3
[2,] Numeric,20 3
[3,] Numeric,20 3
[[1]][[4]]
first second
[1,] Numeric,20 4
[2,] Numeric,20 4
[3,] Numeric,20 4
[[1]][[5]]
first second
[1,] Numeric,20 5
[2,] Numeric,20 5
[3,] Numeric,20 5
[[2]]
[[2]][[1]]
first second
[1,] Numeric,20 1
[2,] Numeric,20 1
[3,] Numeric,20 1
[[2]][[2]]
first second
[1,] Numeric,20 2
[2,] Numeric,20 2
[3,] Numeric,20 2
[[2]][[3]]
first second
[1,] Numeric,20 3
[2,] Numeric,20 3
[3,] Numeric,20 3
[[2]][[4]]
first second
[1,] Numeric,20 4
[2,] Numeric,20 4
[3,] Numeric,20 4
[[2]][[5]]
first second
[1,] Numeric,20 5
[2,] Numeric,20 5
[3,] Numeric,20 5
[[3]]
[[3]][[1]]
first second
[1,] Numeric,20 1
[2,] Numeric,20 1
[3,] Numeric,20 1
[[3]][[2]]
first second
[1,] Numeric,20 2
[2,] Numeric,20 2
[3,] Numeric,20 2
[[3]][[3]]
first second
[1,] Numeric,20 3
[2,] Numeric,20 3
[3,] Numeric,20 3
[[3]][[4]]
first second
[1,] Numeric,20 4
[2,] Numeric,20 4
[3,] Numeric,20 4
[[3]][[5]]
first second
[1,] Numeric,20 5
[2,] Numeric,20 5
[3,] Numeric,20 5
(This look similar, but it failed too.)
How can i solve this problem?