function Life1D(ruleNum, key)

key1 = key;

ruleNum1 = ruleNum;

rule = toBinary(ruleNum);

initialRow = firstRow(ruleNum, key);

disp(['Life1D ' num2str(2*key + 1) ' ' num2str(key + 1)])

makePattern(initialRow, key, rule)

function h = toBinary(n)

h = fliplr(de2bi(n,8));

end

function m = toDecimal(binaryString)

m = bi2de(fliplr(binaryString));

end

function k = firstRow(ruleNum1, key1)

k = [zeros(1 ,key1), 1, zeros(1, key1)];

end

function x = output(input, rule)

i = toDecimal(input);

% x = fliplr(rule);

if i == 0

x = rule(end);

elseif i < 0

x = rule(abs(i)-1);

elseif i > 0

x = rule(end-abs(i));

elseif i == -1

x = rule(1);

end

x

end

function y = makePattern(prevRow, key, rule)

disp(num2str(firstRow(ruleNum, key)))

y = num2str(prevRow);

for i = 0:key

curRow = zeros(1,length(prevRow));

for j = 1:length(prevRow)

if j == 1

curRow(j) = output([0, prevRow(1), prevRow(2)], rule);

elseif j == length(prevRow)

curRow(j) = output([prevRow(end-1), prevRow(end), 0], rule);

else

curRow(j) = output([prevRow(j-1), prevRow(j), prevRow(j+1)], rule);

end

end

prevRow = curRow;

curRow = num2str(curRow);

y = [y;curRow];

end

end

end

More Clarissa Jackson's questions See All
Similar questions and discussions