: .
, ...
'position',[300,400,50,20], ...
'callback','stop=1;close;');
number = uicontrol('style','text', ...
'string','1', ...
'fontsize',12, ...
'position',[20,400,50,20]);
经过对控件(和 CA)初始化,程序进入一个循环,该循环测试由回调函数的每
个按钮控制的变量。刚开始运行时,只在嵌套的 while 循环和 if 语句中运行。
直到退出按钮按下时,循环停止。另外两个按钮按下时执行相应的 if 语句。
stop= 0; %wait for a quit button push
run = 0; %wait for a draw
freeze = 0; %wait for a freeze
while (stop==0)
if (run==1)
%nearest neighbor sum
sum(x,y) = cells(x,y-1) + cells(x,y+1) + ...
cells(x-1, y) + cells(x+1,y) + ...
cells(x-1,y-1) + cells(x-1,y+1) + ...
cells(3:n,y-1) + cells(x+1,y+1);
% The CA rule
cells = (sum==3) | (sum==2 & cells);
%draw the new image
set(imh, 'cdata', cat(3,cells,z,z) )
%update the step number diaplay
stepnumber = 1 + str2num(get(number,'string'));
set(number,'string',num2str(stepnumber))
end
if (freeze==1)
run = 0;
freeze = 0;
end
drawnow %need this in the loop for controls to work
end例子
1 .Conway 的生命游戏机。
规则是:
¾ 对周围的 8 个近邻的元胞状态求和
¾ 如果总和为 2的话,则下一时刻的状态不改变
¾ 如果总和为 3 ,则下一时刻的状态为 1
¾ 否则状态= 0
核心代码:
x = 2:n-1;
y = 2:n-1;
%nearest neighbor sum
sum(x,y) = cells(x,y-1) + cells(x,y+1) + ...
cells(x-1, y) + cells(x+1,y) + ...
cells(x-1,y-1) + cells(x-1,y+1) + ...
cells(3:n,y-1) + cells(x+1,y+1);
% The CA rule
cells = (sum==3) | (sum==
元胞自动机与Matlab 来自淘豆网www.taodocs.com转载请标明出处.