Skip to content

Instantly share code, notes, and snippets.

@gidili
Created March 7, 2010 02:58
Show Gist options
  • Save gidili/324118 to your computer and use it in GitHub Desktop.
Save gidili/324118 to your computer and use it in GitHub Desktop.
% the size of our array of cells
latticeSize=149;
% neighborhood radius
radius = 1;
% rule size
ruleSize=3;
% number of time steps
max=320;
a=zeros(1,latticeSize);
newa=zeros(1,latticeSize);
% the ruleset data object
ruleSet = CARuleset;
% patterns
ruleSet.patterns = flipud(combn([0 1],ruleSize));
% transformation rules for each pattern
% this is wolfram rule 90 --> http://mathworld.wolfram.com/Rule90.html
%ruleSet.transformations = [0, 1, 0, 1, 1, 0, 1, 0];
% this is wolfram rule 110 --> http://mathworld.wolfram.com/Rule110.html
ruleSet.transformations = [0, 1, 1, 0, 1, 1, 1, 0];
%randomize initial configuration
a = randint(1, latticeSize, [0 1]);
%a(50) = 1;
%assign initial configuration to the grid
GRID(1,:)=a;
% initialize the grid except 1st row (initial configuration)
for i=2:max,
GRID(i,:)=zeros(1,latticeSize);
end
%spit out first frame
spy(GRID)
M(1) = getframe;
%brace yourself - this is the main loop
g=1;
while (g<max),
%run the chosen rule foreach cell for a given time step g
%this example includes a circular lattice simulation
for i=1:latticeSize,
% retrieve pattern in the local 'hood
localPattern = circularSubarray(a, i-radius, i+radius);
% find the transformation rule for the given local pattern
for c=1:2^ruleSize,
if(isequal(ruleSet.patterns(c, :, :), localPattern))
newa(i) = ruleSet.transformations(c);
break
end
end
end
% assign new states
g=g+1;
a=newa;
GRID(g,:)=a;
%push a snapshot to a frame
spy(GRID)
M(g) = getframe;
end
%last frame - final state of the matrix
spy(GRID)
M(max) = getframe;
%playback
movie(M,0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment