%
GridN = 40;
ValN = 50;
IterN = 100;
pp = .8;  qq = 1-pp;
% Initialize
Grid = cell(GridN,GridN);
%
for n=1:GridN
    for m=1:GridN
        Grid{n,m} = rand(ValN,1);
    end
end
%
OGrid = Grid; % Original Grid
less1 = [GridN 1:(GridN-1)];
plus1 = [2:GridN 1];
%
for iter=1:IterN    % iteration of overall time steps
    for n=1:GridN
        for m=1:GridN  % loop over grid positions
            i1 = [less1(n) n plus1(n)];  % set up region
            i2 = [less1(m) m plus1(m)];
            for nn=1:3  % cultural adaptation within region
                for mm=1:3  % cell{n,m} influences cell{i1(nn),i2(mm)}
                    choices = (rand(ValN,1)<Grid{n,m});
                    Grid{i1(nn),i2(mm)} = pp*Grid{i1(nn),i2(mm)}+qq*choices;
                end
            end
        end
    end
end
