[R] Array arithmetic

Gang Chen gangchen6 at gmail.com
Thu Mar 6 19:33:30 CET 2008


I have two arrays A and B with dimensions of (L, M, N, P) and (L, M,
N), and I want to do

for (i in 1:L) {
for (j in 1:M) {
for (k in 1:N) {
   if (abs(B[i, j, k]) > 10e-5)  C[i, j, k,] <- A[i, j, k,]/B[i, j, k]
   else C[i, j, k,] <- 0
}
}
}

How can I get C more efficiently than looping?

Thanks,
Gang



More information about the R-help mailing list