% Life in Matlab, by Matt Mets % Configuration dimX = 150; dimY = 150; numRounds = 300; directory = 'images/'; % The next four lines parse and assemble file names extension = '.bmp'; % directory called "images" % Setup board = floor(rand(dimX+2,dimY+2)./.5); board(1,:) = 0; board(dimX+2,:) = 0; board(:,1) = 0; board(:,dimY+2) = 0; % Loop for n=1:numRounds % count neighbors for x=2:dimX+1 for y=2:dimY+1 count(x-1,y-1) = board(x-1,y-1) + board(x,y-1) + board(x+1,y-1)... + board(x-1,y) + board(x+1,y)... + board(x-1,y+1) + board(x,y+1) + board(x+1,y+1); end end % modify the cell for x=2:dimX+1 for y=2:dimY+1 if count(x-1,y-1) <= 1 % die of underpopulation board(x,y) = 0; elseif count(x-1,y-1) == 2 % no change elseif count(x-1,y-1) == 3 % lives/reborn board(x,y) = 1; else % die of overpopulation board(x,y) = 0; end end end % draw the board % colormap([1 1 1; 0 0 1]); % image(board+1); % pause % The following code lifted from the tutorial: % % Creating Animated Gifs and AVIs from the Matlab “Getframe” Command % by Richard Hagl % % http://uwcem.ece.wisc.edu/creatingAnimatedGifs.htm % % Draw the board colormap([1 1 1; 0 0 1]); image(board+1); % set the graph as the target of getframe()? set(gca,'nextplot','replacechildren'); % Image making time Image = getframe; % get current pseudo color image % M(:,n) = Image; % store the current image in an array (this is % % only needed for playback in Matlab) P = frame2im(Image); % Convert to a image representation that Matlab can handle filename = sprintf('%s%06i%s',directory,n,extension); imwrite(P,eval('filename'), 'bmp') % Finally write the individual images end