I am trying to use nested loops to iterate through a [100,12,12] array and modify each element. I am just getting an output array of all NA values. This is what my function looks like:
fcs = function(xg,wg,Gamma_d_dir,Gamma_d_dif,I_o_uc_d,I_o_uc_u,I_d_uc_d,I_d_uc_u) {
Q = array(data=NA,dim=c(3,2,2))
for (i in 1:2) {
for (j in 1:2) {
for (k in 1:3) {
Q[k,j,i] = (1/pi)*Gamma_d_dir[j,i]*I_o_uc_d[k]
}
}
}
#return(Q)
#first collision source due to I_o_uc_u, I_d_uc_d, I_d_uc_u
for (i in 1:2) {
for (j in 1:2) {
for (k in 1:3) {
upperlimit1 = 1
lowerlimit1 = -1
conv11 = (upperlimit1-lowerlimit1)/2
sum1 = 0
for (n in 1:2) {
upperlimit2 = 2*pi
lowerlimit2 = 0
conv21 = (upperlimit2-lowerlimit2)/2
sum2 = 0
for (m in 1:2) {
sum2 = sum2 + wg[m]*(1/pi)*Gamma_d_dif[m,n,j,i]*(I_o_uc_u[k,m,n]+I_d_uc_d[k,m,n]+I_d_uc_u[k,m,n])
}
sum1 = sum1 + wg[n]*sum2*conv21
}
Q[k,j,i] = Q[k,j,i] + sum1*conv11
}
}
}
return(Q)
}
Within the first section of the function, if I return the first Q (the line that is commented out), I get the expected output, an array of size (100,12,12) with actual values. I have also tried returning sum1
to see if that was the problem, but sum1
gives me the expected output as well. However, when I try to return the new array Q[k,j,i] = Q[k,j,i] + sum1*conv11
I just get an array of all NAs.
I apologize that the code will not be reproducible because it draws on other functions/values. I am hoping that it is an easy fix (I'm fairly new to R). Thanks to everyone in advance, this has me completely stuck!
EDIT: Adding data for reproducing
wg = [0.047,0.107,0.160]
xg = [-0.982,-0.904,-0.769]
Gamma_d_dir: structure(c(0.0146368656858818, 0.0146368656858818, 0.0146368656858818,
0.0139717386533205, 0.0139717386533205, 0.0139717386533205, 0.0122837610888392,
0.0122837610888392, 0.0122837610888392), .Dim = c(3L, 3L))
Gamma_d_dif: structure(c(0.0538555492258912, 0.0538555492258912, 0.0538555492258912,
0.049237490337784, 0.049237490337784, 0.049237490337784, 0.041929467456842,
0.041929467456842, 0.041929467456842, 0.0538555492258912, 0.0538555492258912,
0.0538555492258912, 0.049237490337784, 0.049237490337784, 0.049237490337784,
0.041929467456842, 0.041929467456842, 0.041929467456842, 0.0538555492258912,
0.0538555492258912, 0.0538555492258912, 0.049237490337784, 0.049237490337784,
0.049237490337784, 0.041929467456842, 0.041929467456842, 0.041929467456842,
0.0538555492258912, 0.0538555492258912, 0.0538555492258912, 0.049237490337784,
0.049237490337784, 0.049237490337784, 0.041929467456842, 0.041929467456842,
0.041929467456842, 0.0538555492258912, 0.0538555492258912, 0.0538555492258912,
0.049237490337784, 0.049237490337784, 0.049237490337784, 0.041929467456842,
0.041929467456842, 0.041929467456842, 0.0538555492258912, 0.0538555492258912,
0.0538555492258912, 0.049237490337784, 0.049237490337784, 0.049237490337784,
0.041929467456842, 0.041929467456842, 0.041929467456842, 0.0538555492258912,
0.0538555492258912, 0.0538555492258912, 0.049237490337784, 0.049237490337784,
0.049237490337784, 0.041929467456842, 0.041929467456842, 0.041929467456842,
0.0538555492258912, 0.0538555492258912, 0.0538555492258912, 0.049237490337784,
0.049237490337784, 0.049237490337784, 0.041929467456842, 0.041929467456842,
0.041929467456842, 0.0538555492258912, 0.0538555492258912, 0.0538555492258912,
0.049237490337784, 0.049237490337784, 0.049237490337784, 0.041929467456842,
0.041929467456842, 0.041929467456842), .Dim = c(3L, 3L, 3L, 3L
))
I_o_uc_d: c(1.38029388938415, 1.34170990124015, 1.3042044690128, 1.26774744333388
)
I_o_uc_u: structure(c(0.000129755423480137, 0.000133102190791582, 0.000136535280902773,
0.000129749291942523, 0.000133095964315359, 0.000136528958669636,
0.000129755205207249, 0.000133101969139083, 0.000136535055841534,
NA, NA, NA, 0.000110308754382282, 0.000110426689291562, 0.000110426689291562,
0.000540092124278889, 0.000554022660808504, 0.000568312506128747,
0.000540080966347844, 0.000554011478199665, 0.000568301304992292,
0.000540092124278889, 0.000554022660808504, 0.000568312506128747,
NA, NA, NA), .Dim = c(3L, 3L, 3L))
I_d_uc_d: structure(c(0.0922248307669918, 0.0860202770252447, 0.0802331432647984,
0.0921979665137745, 0.0859451281500086, 0.0801163554037587, 0.0922289322269716,
0.0860317541213116, 0.0802509856557281, 0.00757742076841967,
0.00757741721085118, 0.00757739979310332, NA, NA, NA, 0.00186644063217427,
0.00174087324309048, 0.00162375357472682, 0.00180599655247098,
0.00168351440937043, 0.0015693389683829, 0.00186644063217427,
0.00174087324309048, 0.00162375357472682, 0.00386349245322398,
0.00388211129262134, 0.00383047289113047), .Dim = c(3L, 3L, 3L
))
I_d_uc_u: structure(c(6.03810540123238e-05, 6.19384558717535e-05, 6.35360276253899e-05,
6.03782007311746e-05, 6.19355584113849e-05, 6.35330856050081e-05,
6.03809524400993e-05, 6.19383527268434e-05, 6.35359228942392e-05,
NA, NA, NA, 5.1331641312152e-05, 5.13865217474828e-05, 5.13865217474828e-05,
0.000251329238139372, 0.000257811745429282, 0.000264461455313982,
0.000251324045850533, 0.000257806541656752, 0.000264456242919717,
0.000251329238139372, 0.000257811745429282, 0.000264461455313982,
NA, NA, NA), .Dim = c(3L, 3L, 3L))
sum1 = 3.61e-05
This is what the first return of Q looks like:
structure(c(0.0064308707377685, 0.00625110565859951, 0.00607636563513547,
0.0064308707377685, 0.00625110565859951, 0.00607636563513547,
0.00613863973268913, 0.00596704352083312, 0.00580024401658751,
0.00613863973268913, 0.00596704352083312, 0.00580024401658751
), .Dim = c(3L, 2L, 2L))
, , 1
[,1] [,2]
[1,] 0.006430871 0.006430871
[2,] 0.006251106 0.006251106
[3,] 0.006076366 0.006076366
, , 2
[,1] [,2]
[1,] 0.006138640 0.006138640
[2,] 0.005967044 0.005967044
[3,] 0.005800244 0.005800244