function [out,diff]=CA_1d(n,rule,p,transient,time)
%n   number of lattive sites
%time  number of temporal iterations in out
%rule  number of the elementary cellular automaton according to the usual code
%p     probability of 1's in the random initial condition
%out   spatio-temporal pattern n*time of the state variables
%diff  pattern of state differences after changing the state of one cell


%initialize the arrays
row = (rand(1,n) > p);
% p kontrolliert die Zahl der Einsen in der Anfangsbedingung)
cells = zeros(n,n);
cells(1,1:n)=row;
z=zeros(n,n);

%Matrix fuer Ausertung der Rule - erstmal fuer r=3
select=[[n,1,2]];
vector=[1,2,3];
for j=1:n-2
    select=[select;vector];
    vector=vector+1;
end
select=[select;[n-1,n,1]];

nb=2^3;
rule_bits=bitget(rule,1:nb);

for k=1:transient    
        row=rule_bits(row(select)*[4,2,1]'+1);
end 
out=zeros(time,n);
diff=zeros(time+1,n);
row2=row;
row2(floor(n/2))=~row(floor(n/2));
diff(1,:)=row-row2;
for k=1:time
    row=rule_bits(row(select)*[4,2,1]'+1);
    row2=rule_bits(row2(select)*[4,2,1]'+1);
    out(k,:)=row;
    diff(k+1,:)=row-row2;
end
figure(1);imagesc(out);ylabel('time');
figure(2);imagesc(diff);ylabel('time');title('difference')
    
    
