필터 지우기
필터 지우기

Need help with Game of life

조회 수: 6 (최근 30일)
Daniel Hodgson
Daniel Hodgson 2020년 8월 27일
답변: Vimal Rathod 2020년 8월 31일
%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