function  cellasorok  = ca1_sim(cells, rule, iter)
% gets 'cells' binary input (row) vector of size n,
% makes iteration for 'iter' with periodic boundary conditions
% w_rule defines Wolfram 1D cellular automata rule (decimal value)
% outputs 'iter+1 x n+2' size of binary matrix, each row is the next iterated state 
% white cells: 0, black cells: 1

brule = dec2bin(rule);
while length(brule)<8
	brule=strcat('0',brule);
end	

lcells = [ cells(end) cells cells(1) ];
cellasorok = [ lcells;
               zeros(iter,length(lcells)) ];
for i=2:iter+1
    for j=2:length(cells)+1
        index = 8 - cellasorok(i-1,j-1)*4 - cellasorok(i-1,j)*2 - cellasorok(i-1,j+1)*1;
        cellasorok(i,j)=str2double(brule(round(index)));
    end
    cellasorok(i,1) = cellasorok(i,end-1);
    cellasorok(i,end) = cellasorok(i,2);
end
image(~cellasorok(:,2:end-1)*8);
colormap(gray(2));
% set(gca,'xtick',0.5:1:length(cells)+0.5,'ytick',0.5:1:iter+0.5)
% grid on
