Problem with my cellular automata model

1 view (last 30 days)
Naga Harish
Naga Harish on 28 Oct 2014
Answered: Geoff Hayes on 30 Oct 2014
clear all;
clc;
%%Creating a grid with random value
n = 64;
Gpop = rand(n,n);
temp=Gpop;
Gpop(temp(:,:)<0.99) = 1; %Healthy percentage 99%
Gpop(temp(:,:)>0.99 & temp(:,:)<0.994) = 2; %Healthy percentage .04%
Gpop(temp(:,:)>0.994 & temp(:,:)<0.998) = 3; %Healthy percentage .04%
Gpop(temp(:,:)>0.998) = 4; %Healthy percentage .02%
%%Our Rules of cellular automata
x = 2:n-1; % Intializing x and y values to access the cells of CA
y = 2:n-1;
rule = Gpop;
figure
count=0;
time = 0;
while(count<25)
rule((rule(x-1,y-1)==2)|(rule(x,y-1)==2)|(rule(x+1,y-1)==2)|(rule(x-1,y)==2)|(rule(x+1,y)==2)...
|(rule(x-1,y+1)==2)|(rule(x,y+1)==2)|(rule(x+1,y+1)==2) & time==1)=2 ; %1st Rule a
if((rule(x,y-1)==3)| (rule(x-1,y)==3)|(rule(x+1,y)==3)|(rule(x,y+1)==3) & time ==2);
rule(x,y)==2;
else((rule(x-1,y-1)==3)|(rule(x+1,y-1)==3)|(rule(x-1,y+1)==3)|(rule(x+1,y+1)==3) & time ==3);
rule(x,y)==2;
end
rule((rule(x-1,y-1)==3)|(rule(x,y-1)==3)|(rule(x+1,y-1)==3)|(rule(x-1,y)==3)|(rule(x+1,y)==3)...
|(rule(x-1,y+1)==3)|(rule(x,y+1)==3)|(rule(x+1,y+1)==3) & time==4)=3; %2nd rule
rule((rule(x-1,y-1)==4)|(rule(x,y-1)==4)|(rule(x+1,y-1)==4)|(rule(x-1,y)==4)|(rule(x+1,y)==4)...
|(rule(x-1,y+1)==4)|(rule(x,y+1)==4)|(rule(x+1,y+1)==4&time==6))=4; %3rd rule
newMatrix=rand(n,n);
newtemp=newMatrix;
newMatrix(newtemp(:,:)<=.1)=1;
newMatrix(newtemp(:,:)>.1)=0;
rule(((rule(x-1,y-1)==4)|(rule(x,y-1)==4)|(rule(x+1,y-1)==4)|(rule(x-1,y)==4)|(rule(x+1,y)==4)...
|(rule(x-1,y+1)==4)|(rule(x,y+1)==4)|(rule(x+1,y+1)==4)) & newMatrix(x,y)==1 & time == 8)=1; %1st part 4th rule
rule(((rule(x-1,y-1)==4)|(rule(x,y-1)==4)|(rule(x+1,y-1)==4)|(rule(x-1,y)==4)|(rule(x+1,y)==4)...
|(rule(x-1,y+1)==4)|(rule(x,y+1)==4)|(rule(x+1,y+1)==4)) & newMatrix(x,y)==0 & time == 10)=2; %1st part 4th rule
imagesc(rule)
axis off;
cmap = jet(4); % assign colormap
colormap(cmap)
hold on
L = line(ones(4), ones(4), 'LineWidth',2); % generate line
set(L,{'color'},mat2cell(cmap,ones(1,4),3)); % set the colors according to cmap
legend('H','I1','I2','D') %Addings Legends at the top right corner of image
count=count+1;
time = time+1;
pause(3.0)
end
This is my cellular automata code, thing is when i run this code right of the automata remains as it is without any changes. Can anybody please help me with this. I am unable to understand where i am going wrong. Thanks in advance.
  2 Comments
Geoff Hayes
Geoff Hayes on 28 Oct 2014
Naga - I ran your code and the image did change every three seconds for 25 iterations of the while loop. If you wish to see it update quicker, then just replace the time of the pause from three seconds to one second.
Naga Harish
Naga Harish on 28 Oct 2014
Hello Geoff Hayes the problem is not with the time but if u ran and see it again right side of the image remains as it is and the cells wont change they remain in initial state please help me with that and i am not asking about image changes. Thanks in advance.

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 30 Oct 2014
Naga - you may want to comment your code and revisit your use of the (for example) time==1 as a condition, as it doesn't seem to do anything. Also, in your if block, if the condition evaluates to true, you do
rule(x,y)==2;
where == is used instead of =. This is true in the else as well.
It isn't clear why you use the time conditions - why update the rule only on certain time steps?

Community Treasure Hunt

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

Start Hunting!