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細胞: (ⅰ)至少一種I1小區在最近的鄰居或第二個最近的鄰居; (ii)最近的鄰居中至少有x個I2個單元,第二個最近的鄰居中的I2個單元。
規則2:I1單元在下一步中變爲I2單元。規則3:由於免疫識別和清除,I2步驟在τ步驟後變成D細胞。規則4:D細胞可以被概率爲Pinf的I1細胞替換,或者在下一步中被概率爲(Prep - Pinf)的H細胞替換。
我想知道我的代碼是否符合這些規則,以及爲了正確模擬病毒,我必須在代碼中進行哪些更改。請任何人幫我解決這個問題。在此先感謝
你是什麼意思_右側細胞保持原樣,沒有任何變化_? – 2014-10-28 22:48:13
我的意思是在模擬發生後,第60列的單元格保持不變。它就像在你運行代碼之後,圖像就會隨着它的變化而出現。如果你正確地觀察圖像,圖像的右側保持原樣而沒有任何變化,但是不應該發生,我只想知道我的代碼中的缺陷以及我應該對模擬這種元胞自動機所做的更改。請幫我困在中間,我很無能。 – Hari 2014-10-29 03:22:34