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以上是模拟HIV病毒4个阶段的元胞自动机代码。当我运行上面的代码时,右边的单元格保持不变,没有任何变化,我非常努力地找出错误的地方,但也无法。
以下是我自动机的规则,
规则1:如果H单元满足下面列出的规则中的至少一个,则它在下一步成为I1单元:(i)在最近邻或第二最近邻中至少有一个I1单元;(ii)最近邻中的至少x个I2单元,第二最近邻中的y I2单元。
规则2:在下一步中,I1单元格变为I2单元格。
规则3:由于免疫识别和清除,I2细胞在τ步骤后变为D细胞。
规则4:下一步,D细胞可以用概率Pinf的I1细胞代替,也可以用带有概率的H细胞(Prep−Pinf)代替。
我想知道我的代码是否符合这些规则,以及为了正确模拟病毒,我必须在代码中做哪些更改。请任何人帮我解决这个问题。提前感谢
发布于 2014-10-29 03:17:49
您的问题是,当您在每个节点的8个邻居上测试规则时,0-1决策矩阵是62*62 (因为您设置了x/y = 2:n-1),然后0/1设置为规则矩阵,因此最后两列始终保持不变,因为您从未“触摸”它们!
要理解我的意思,只要在任何规则上设置一个断点即可。
(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)通过打印上述结果,您将发现它是一个62*62矩阵。
我知道你想用矩阵计算来简化代码,同时避免边界问题。但是现在我想不出更好的解,除非通过x和y设置为循环,如果点在边界上,只需使用3或5个邻域。
另一种方法是创建“松弛”行和列,如rule.size()=66*66,并将边界设置为零,然后在绘图时只需丢弃松弛的行和列。
希望这能有所帮助。
https://stackoverflow.com/questions/26618868
复制相似问题