Life in Matlab

I’ve always wanted to make a life simulator, so here is a quick and dirty one using Matlab. I think the rectangle drawing takes up a significant portion of the CPU time…

Source Code after break.

UPDATE: I posted an animated gif of the program running:

% Life in Matlab, by Matt Mets

% Configuration
    dimX = 100;
    dimY = 100;
    numRounds = 200;

% 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
        clf
        axis([1 dimX+1 1 dimY+1])
        for x=2:dimX+1
            for y=2:dimY+1
                if board(x,y) == 1
                    rectangle('Position', [x-1 y-1 1 1], ...
                       'facecolor','b');
                end
            end
        end
        pause 
    end
This entry was posted in Journal, tech. Bookmark the permalink.

6 Responses to Life in Matlab

  1. Kevin says:

    Coolness, a quick suggestion to speed display is to use images instead of plots.

    Modified source:
    http://www.dewgatherer.net/matthew/life.m.diff

    Patch file:
    http://www.dewgatherer.net/matthew/life.m.new

  2. Kevin says:

    A side benefit is it runs on Octave (doesn’t support rectangle function)

  3. mahto says:

    I took the image suggestion you made and rolled it into my program, and also added the capability to export bitmaps of each of the frames so that they can be made into an animated gif. I used ImageMagick to compile them into a movie:

    convert *.bmp anim.gif

    It should be possible to make an animated gif completely in Matlab, but I was too lazy. My modified source is here. I will update the original post with a link to an animation…

  4. mahto says:

    Ah, the image trick is a neat one, thanks for sharing! Although on my version of Matlab (R14), it didn’t like having two parameters, so I had to change the line to this:

    image(board+1);

  5. Matt says:

    I was playing with your code and added some statistical outputs as well as color changes for newborns and elders. I don’t have the 1337 coding skills of you guys though. When I tried to actually make an M-file of it I wasn’t allowed to. It turns out that Matlab, at least as of version 7.1, has a life simulator built in. It runs fast and wraps the borders.

  6. mahto says:

    Hey, statistical outputs, cool! What I would like to do is add a scanner for recognized stable structures, and have it produce a count of each kind per frame. I believe that the stability calculation is an incomputable problem in general, but it would be fun to develop an algorithm to at least pattern-match them from a database; even better would be to have it build a catalog of lifeforms automatically…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>