Matlab code considerations

The following considerations will be illustrated using a Matlab program which computes Conway's life. Parts of the code are explained below.

  • Matrixes and images can be converted to one-another, so display is straighforward. If the array cells contains the binary state of each cell and the array z contains zeros, then the cat command builds an RGB image, which is displayed by the image command. The image command also returns a handle.
    imh = image(cat(3,cells,z,z));
    set(imh, 'erasemode', 'none')
    axis equal
    axis tight
      
  • Matrixes and images can be converted to one-another, so initial conditions may be computed by matrix or graphics commands. The following code generates an array of zeros, initializes the cell state to zero, then makes a cross of cells in state=1 in the center of the array. One of the examples below (percolation cluster) uses graphics commands to initialize the CA array.
    z = zeros(n,n);
    cells = z;
    cells(n/2,.25*n:.75*n) = 1;
    cells(.25*n:.75*n,n/2) = 1;
    	
  • The Matlab code should be vectorized to minimize overhead. The following two statements compute the sum of nearest neighbors and compute the CA rule. The code makes use of Matlab's very flexible indexing to specify the nearest neighbors.
    x = 2:n-1;
    y = 2:n-1;
    sum(x,y) = cells(x,y-1) + cells(x,y+1) + ...
                cells(x-1, y) + cells(x+1,y) + ...
                cells(x-1,y-1) + cells(x-1,y+1) + ...
                cells(x+1,y-1) + cells(x+1,y+1);
    cells = (sum==3) | (sum==2 & cells);  
      
  • Adding a simple GUI is easy. In this example three buttons and a text field were implmented. The three buttons allow a user to Run, Stop, or Quit. The text field displays the number of simulation steps executed.
    %build the GUI
    %define the plot button
    plotbutton=uicontrol('style','pushbutton',...
       'string','Run', ...
       'fontsize',12, ...
       'position',[100,400,50,20], ...
       'callback', 'run=1;');
    
    %define the stop button
    erasebutton=uicontrol('style','pushbutton',...
       'string','Stop', ...
       'fontsize',12, ...
       'position',[200,400,50,20], ...
       'callback','freeze=1;');
    
    %define the Quit button
    quitbutton=uicontrol('style','pushbutton',...
       'string','Quit', ...
       'fontsize',12, ...
       'position',[300,400,50,20], ...
       'callback','stop=1;close;');
    
    number = uicontrol('style','text', ...
        'string','1', ...
       'fontsize',12, ...
       'position',[20,400,50,20]);
    	
    After the controls (and CA) are initialized, the program drops into a loop which tests the state of the variables which are set in the callback functions of each button. For the moment, just look at the nested structure of the while loop and if statements. The program loops until the Quit button is pushed. The other two buttons cause an if statement to execute when pushed.
    stop= 0; %wait for a quit button push
    run = 0; %wait for a draw 
    freeze = 0; %wait for a freeze
    while (stop==0) 
           if (run==1)
    	      %nearest neighbor sum
    	      sum(x,y) = cells(x,y-1) + cells(x,y+1) + ...
    		            cells(x-1, y) + cells(x+1,y) + ...
    		            cells(x-1,y-1) + cells(x-1,y+1) + ...
    		           cells(3:n,y-1) + cells(x+1,y+1);
    	      % The CA rule
    	      cells = (sum==3) | (sum==2 & cells);       
    	      %draw the new image
    	      set(imh, 'cdata', cat(3,cells,z,z) )
    	      %update the step number diaplay
    	      stepnumber = 1 + str2num(get(number,'string'));
    	      set(number,'string',num2str(stepnumber))
          end
          if (freeze==1)
    	     run = 0;
    	     freeze = 0;
          end
          drawnow  %need this in the loop for controls to work  
    end
    	

Examples

  1. Conway's life.
    The rule is:
    • Sum the 8 nearest neighbors
    • If the sum=2 then the state does not change
    • If the sum=3 then the state=1
    • Otherwise the state=0
    The code:
    x = 2:n-1;
    y = 2:n-1;
      %nearest neighbor sum
    sum(x,y) = cells(x,y-1) + cells(x,y+1) + ...
    		   cells(x-1, y) + cells(x+1,y) + ...
    		   cells(x-1,y-1) + cells(x-1,y+1) + ...
    		   cells(3:n,y-1) + cells(x+1,y+1);
    % The CA rule
    cells = (sum==3) | (sum==2 & cells); 
      
  2. Surface Tension
    The rule is:
    • Sum the 8 nearest neighbors and the cell itself
    • If the sum< 4 or sum=5 then the state=0
    • Otherwise the state=1
    The code:
    x =...
Read more »