## Hello I have this code for Game of life now I am getting an error in calculating the num_neighbors. Please help me fix it. Thank you

on 27 Oct 2017
Latest activity Answered by Aveek Podder

### Aveek Podder (view profile)

on 7 Nov 2017
mylife = round(rand(size_of_game,size_of_game));
%mylife = [0 0 0 0 0;0 0 0 0 0;0 1 1 1 0;0 0 0 0 0 ;0 0 0 0 0 ];
%%%plot
while 1%for 11 = 1:1000
axis([-size_of_game size_of_game -size_of_game size_of_game])
cla;
spy(mylife)
grid on
%propagate
newlife = mylife;
for ii = 1:size_of_game
si = ii-1;
ei = ii+1;
veci = si:ei;
veci(veci==0)=size_of_game;
veci(veci==size_of_game)=1;
for jj = 1:size_of_game
%%%This is overlap from each side
sj = jj-1;
ej = jj+1;
vecj = sj:ej;
vecj(vecj==0)=size_of_game;
vecj(vecj==size_of_game+1)=1;
num_neighbors =sum(sum(mylife(veci,vecj)))-mylife(ii,jj);
if mylife(ii,jj)
%Any live cell with fewer than 4 live neighbours dies
if num_neighbors <= 4
newlife(ii,jj) = 0;
end
%Any live cell with four or more live neighbors lives on to
%next generation
if num_neighbors > 4
newlife(ii,jj) = 1;
end
end
end
end
drawnow
mylife=newlife;
end