Need help with Game of life

7 次查看(过去 30 天)
%I have school project which requires to programme Game of life. What im looking for is tips and trick to how to imporve my current code, im quite new to matlab and this is only the begining of my project but im getting stuck at some place and would really appriciate some help. I have tried to be as clear as possible.
clc, clear all
%figure below is mandatory starting condition
b=zeros(16) ;%matrix all zeros
b(5:9,5)=1 ;% left ones
b(7:11,11)=1 ;%right ones
b(5,7:11)=1 ;%top ones
b(11,5:9)=1; %bottom ones
spy(b,40,'g'); % plot picture
a=b;
[y,x]=find(a)%helps find all ones in matrix
for i=1:length(x) %trying to calcule neighbours around each cell containing one
c=a(y(i)+1,x(i))+a(y(i)-1,x(i))+a(y(i),x(i)+1)+a(y(i),x(i)-1)...
+a(y(i)+1,x(i)+1)+a(y(i)+1,x(i)-1)+a(y(i)-1,x(i)+1)+a(y(i)-1,x(i)-1)
%adds up all neighbour cell
%WANT TO DO: plug c values into vector then change vector(vales) with conditions below
%then plug back into matrix a replacing the numbers that were there before
if c<2
a(y(i),x(i))=0
elseif c or(2, 3)
a(y(i),x(i))=1
elseif c>3
a(y(i),x(i))=0
end
end

采纳的回答

Vimal Rathod
Vimal Rathod 2020-8-31
You could use the conv2 function to find out the elements in the neighbours and then calculate the sum directly.
Below is a link for an answer which could be of use to you.
For more info about conv2 refer to the following link
hope this helps!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Conway's Game of Life 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by