Borrar filtros
Borrar filtros

Need help with Game of life

6 visualizaciones (últimos 30 días)
Daniel Hodgson
Daniel Hodgson el 27 de Ag. de 2020
Respondida: Vimal Rathod el 31 de Ag. de 2020
%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

Respuesta aceptada

Vimal Rathod
Vimal Rathod el 31 de Ag. de 2020
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!

Más respuestas (0)

Categorías

Más información sobre Conway's Game of Life en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by