I am trying to make a code for sandpiles in R, but after an attempt to make the code faster, it just stopped working.
It is telling that The matrix in which the sandpile is stored isn't defined. I tried defining it in different ways, but each threw the same error in different lines. Below is my code.
hg=301
wd=301
currpile<<-matrix(c(0),nrow=wd,ncol=hg)
currpile[151,151]=999999
while(max(currpile)>3){
for (y in 1:hg) {
for (x in 1:wd){
if (currpile[y,x]>=4){
currpile[y,x]<<-currpile[y,x]-4
if(y+1<=hg){
currpile[y+1,x]<<-currpile[y+1,x]+1
}
if(y-1>0){
currpile[y-1,x]<<-currpile[y-1,x]+1
}
if(x+1<=wd){
currpile[y,x+1]<<-currpile[y,x+1]+1
}
if(x-1>0){
currpile[y,x-1]<<-currpile[y,x-1]+1
}
}
}
}
}
library(raster)
plot(raster(currpile))
and the error it is showing:
object 'currpile' not found