Advertisement
HM97

Untitled

Nov 18th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.07 KB | None | 0 0
  1. clear all;
  2. A=[0 0 0 0 0; 0 1 1 1 0; 0 0 1 1 1; 0 0 0 0 0; 0 0 0 0 0];
  3. %disp('Generation-0:');
  4. %disp(A);
  5. %s = size(A);
  6. %B = zeros(size(A));
  7. %n = numel(A);
  8. %out = cell(s);
  9. gen1=zeros(5,5);
  10. igenaration=5;
  11.  
  12. for k=1:igenaration-1
  13.    
  14.     disp(A);
  15.     imshow(A);
  16.     drawnow;
  17.     s = size(A);
  18.     B = zeros(size(A));
  19.     n = numel(A);
  20.     out = cell(s);
  21.  
  22.     for i = 1:n
  23.       B(i) = 1;
  24.       %out{i} = A(bwdist(B,'ch') == 1);
  25.       out{i}=A(conv2(B,[1,1,1;1,0,1;1,1,1],'same')>0);
  26.       B(i) = 0;
  27.       n=sum(out{i},'all');
  28.       %n = nnz(out{i}==1);%Number of nonzero matrix elements
  29.  
  30.       %sum(out{i}(:) == 1);
  31.       %disp(count(out{i}));
  32.       if B(i)==1 %for current cell- live cell
  33.         if n<2
  34.           gen1(i)=0;
  35.         end
  36.         if n==2 || count==3
  37.           gen1(i)=1;
  38.         end
  39.  
  40.         if n>3
  41.           gen1(i)=0;
  42.         end
  43.         else  %for current cell- dead cell
  44.         if n==3
  45.           gen1(i)=1;
  46.         end    
  47.       end
  48.  
  49.       %disp(n);
  50.     end
  51.     fprintf('Generation- %d:\n',k);
  52.     %disp(gen1);
  53.     A=gen1;
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement