I am trying to find the list of neighbours inside a Moore neighbourhood of variable length in a 2D cellular automata (will run different conditions on them later on). I have attached a link of an image for a Moore neighbourhood with variable r from Wolfram MathWorld, in case anyone is interested. In an NXN grid, my code is as below. This is ok for r = 1; but for r>1, it keeps missing some values. For example, for r = 2, it misses (i-1,j-2);(i+1,j-2);(i+2,j-1);(i+2,j+1);(i+1,j+2);(i-1,j+2);(i-2,j+1);(i-2,j-1).
for i = 1:N for j = 1:N for r = 1:R neighbour_ij = [(i-r,j-r);(i,j-r);(i+r,j-r); (i+r,j);(i+r,j+r);(i,j+r);(i-r,j+r);(i-r,j)]; endend
Best Answer