Problem
Say I have a function that is currently not vectorized. The following is just an example :
FunctionNotVectorized = function(x,y,some_options) return(x[1]+y[1])
which has, say, 10 different options. I would like to
- 1) define a matrix of size
1e5 x 1e5
for each option. - 2) then, for each matrix, assign values for their corresponding indices.
First, I defined a matrix of size 1e5 x 1e5
for each option, by for loop :
for (k in 1:10){
assign(sprintf("res%02d", k), matrix(0,1e5,1e5))
}
which defines matrices named res01
, ... res10
.
Second, I tried to assign values for their corresponding indices for each matrix. But I'm stuck here
Try
What I would like to do:
for (i in 1:1e5){
for (j in 1:1e5){
for (k in 1:10){
assign(sprintf("res%02d[i,j]", k),
FunctionNotVectorized(i,j,some_options=k))
}
}
}
but clearly, assign(sprintf("res%02d[i,j]", k)
does not work. Any help will be appreciated.