% ca_1D.m - one-dimnesional CA with 2 states and 2 neighbors % Feb.9, 2003 % SG clear all close all % PARAMETERS RULE = 54; % rule # cells = 100; % number of cells T = 100; % number of iterations % INITIAL STATE init = round(rand(1,cells)); % random initial states init = zeros(1,cells); % single red in the middle init(cells/2) = 1; % USEFUL rule = bitget(RULE,8:-1:1); % convert RULE to a 1x8 binary array vec = [4 2 1]; X(1,:) = init; tic for t=1:T Y = X(t,:); d(2,:) = Y; d(1,:) = [Y(end) Y(1:(end-1))]; d(3,:) = [Y(2:end) Y(1)]; X(t+1,:) = rule(8-vec*d(:,:)); end toc z = zeros(size(X)); image(cat(3,X,z,z)); axis equal axis tight xlabel('space') ylabel('time') title(['Rule ', num2str(RULE)])