Sample Code for 1d GOL

function theGolProgram()

    % the parameters
    number_of_cells = 80;
    number_of_gens = 100;
    
    % set up the structure to hold the results
    % and give it some initial values
    population = zeros(number_of_gens+1, number_of_cells);
    population(1,10)=1; population(1,40) = 1;

    % the middle - everything else goes here
    for curr_gen = 2:number_of_gens
        
        % do something this many times
        population = apply_rulebook(population, number_of_cells, curr_gen);
        update_screen(population);
        
    end 
end

function update_screen(population)
    % imagesc is the name of a Matlab function which creates a
    % picture from an array of numbers
    imagesc(population);
    drawnow;
end

function population = apply_rulebook(population, number_of_cells, curr_gen) 

    % rule number 1
    last_gen = curr_gen-1;
    for curr_cell = 1:number_of_cells;
        % for live cells only
        if population(last_gen, curr_cell)==1
            population(curr_gen, curr_cell)=0;
        end
    end
    
    % rule number 2
    for curr_cell = 1:number_of_cells;
        % rule 2 applies to dead cells only
        if population(last_gen, curr_cell)==0
            % handle special cases for first and last cell
            % to put population in a ring
            if curr_cell ==1
                leftn = population(last_gen, number_of_cells);
            else
                leftn = population(last_gen, curr_cell-1);
            end
            if curr_cell == number_of_cells
                rghtn = population(last_gen, 1);
            else
                rghtn = population(last_gen, curr_cell+1);
            end
            % only one neighbour?
            % then a cell is born
            if (rghtn + leftn) == 1
                population(curr_gen, curr_cell) = 1;
            end
        end
    end
end