# Easily parallelize R nested for loop with foreach?

I have this triple loop with the output all being written to the same array, asc.a.out. I was wondering if there is a simple way to parallelize this, I tried foreach (now commented out), but the output array was empty. Is there a simple way to speed up constructs like this, I remember in MATLAB loops could simply be converted to parallel loops without any major code changes, much like openmp. The reason my data is within this structure is because it's spatial data with a time series at each location.

edit: I have added a simple example at the end.

asc.a     = array(55,dim=c(120,180,2190)) # filled with nonsense data as an example.
asc.a.out = array(NaN,dim=c(120,180,426))

for (i in 1:120) {
#foreach (i = 1:120) %dopar% {
for (j in 1:180) {

yy = seq(1:2190)

asc.a.b = rbind(asc.a[i,j,1:365],asc.a[i,j,366:730],asc.a[i,j,731:1095],asc.a[i,j,1096:1460],asc.a[i,j,1461:1825],asc.a[i,j,1826:2190])
asc.a.b = cbind(asc.a.b[,335:365],asc.a.b,asc.a.b[,1:30])

asc.a.c = c(asc.a[i,j,1:365],asc.a[i,j,366:730],asc.a[i,j,731:1095],asc.a[i,j,1096:1460],asc.a[i,j,1461:1825],asc.a[i,j,1826:2190])

check = asc.a.c
check = sort(asc.a.c,decreasing = T)
if (sum(!is.na(check[1:365])) < 100) next

model <- rlm(asc.a.c ~ poly(yy,25))
predicted.intervals <- predict(model,data.frame(x=yy),interval='confidence',level=0.99)

asc.a.c.tmp = asc.a.c - predicted.intervals[,1]

sd = sd(asc.a.c.tmp,na.rm=T)
med = median(asc.a.c.tmp,na.rm=T)
q25 = quantile(asc.a.c.tmp,na.rm=T)
asc.a.c.tmp[asc.a.c.tmp < (0-sd)] = NaN
asc.a.c.tmp[asc.a.c.tmp > (0+sd)] = NaN

asc.clim = asc.a.c.tmp + predicted.intervals[,1]
asc.a.d = rbind(asc.clim[1:365],asc.clim[366:730],asc.clim[731:1095],asc.clim[1096:1460],asc.clim[1461:1825],asc.clim[1826:2190])
asc.a.d = cbind(asc.a.d[,335:365],asc.a.d,asc.a.d[,1:30])

for (k in 16:411) {

tmp1 = asc.a.b[,((k-15):(k+15))]
tmp = quantile(asc.a.d[,((k-15):(k+15))],na.rm=T)

tmp1[tmp1 < tmp[2]] = NaN
tmp1[tmp1 > tmp[4]] = NaN

asc.a.out[i,j,k] = mean(tmp1,na.rm=T)
}
}
print(i)
}

edit: smaller example with same code structure as above.

asc.a     = array(55,dim=c(120,180,426))
asc.a.out = array(NaN,dim=c(120,180,426))

for (i in 1:120) {
#foreach (i = 1:120) %dopar% {
for (j in 1:180) {

tmp = asc.a[i,j,]

for (k in 16:411) {
tmp1 = tmp[((k-15):(k+15))]
asc.a.out[i,j,k] = mean(tmp1,na.rm=T)
}
}
print(i)
}