Reputation: 764
Suppose
t=c(0,0.5,0.7,0.9,1,1.2) and
v=matrix(1:40, nrow=5, ncol=8)
v
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 6 11 16 21 26 31 36
[2,] 2 7 12 17 22 27 32 37
[3,] 3 8 13 18 23 28 33 38
[4,] 4 9 14 19 24 29 34 39
[5,] 5 10 15 20 25 30 35 40
I would like to make an array of order 5x6x4.
array <- array(NA, dim = c(5, 6, 4))
5 is the number of rows, 6 is the length of t
and 4 is the number of arrays. to create the first array, I would like to consider only the first two columns of v, and for each time point, the first element of the first matrix is filled by v[1,1]+v[1,2]*t[]
which is the value of the first element of the first matrix of the first array which is the value of array[1,1,1]
, similarly, array[2,1,1]e v[2,1]+v[2,2]*t[1]
For the second array consider only 3rd and 4th column of v. For the third array consider only 5th and 6th column of v, and Finally, for the fourth array, consider the last two columns of v.
I would appreciate if anyone can help me using a for loop or alternative ways?
Thanks
Upvotes: 1
Views: 48
Reputation: 2364
Try these nested loops:
ary <- array(NA, dim = c(5, 6, 4))
for (i in 1:5) {
for (j in 1:6) {
for (k in 1:4) {
ary[i, j, k] <- v[i, k * 2 - 1] + v[i, k * 2] * t[j]
}
}
}
Upvotes: 1