Skip to content

Instantly share code, notes, and snippets.

@chris-taylor
Created June 22, 2012 15:56
Show Gist options
  • Select an option

  • Save chris-taylor/2973671 to your computer and use it in GitHub Desktop.

Select an option

Save chris-taylor/2973671 to your computer and use it in GitHub Desktop.
Simulation of cancer cells
function [plate cellLocs] = simCancer(sz,pDiv,maxCells)
directions = [1 0; -1 0; 0 1; 0 -1]; % Possible grid directions
plate = zeros(sz); % Initial state of the plate
cellLocs = zeros(sz*sz,2); % (x,y) location of cells
numCells = 1; % Initial number of cells
generation = 1; % Initial generation
cellLocs(1,1) = randi(sz); % Place the first cell
cellLocs(1,2) = randi(sz);
plate(cellLocs(1,1),cellLocs(1,2)) = 1;
f = figure;
a = axes('Parent',f);
im = imagesc(plate, 'Parent', a);
colorbar;
colormap gray
% Loop until the number of cells is less than the max possible number
while (numCells < maxCells)
fprintf('Generation: %4d, Number of cells: %4d\n',generation,numCells)
generation = generation + 1;
for n = 1:numCells
curLoc = cellLocs(n,:);
newGen = generation;
if rand() < pDiv
while plate(curLoc(1),curLoc(2)) > 0
oldGen = plate(curLoc(1),curLoc(2)); % bump the old cell
plate(curLoc(1),curLoc(2)) = newGen; % new cell takes old one's place
newGen = oldGen;
direction = directions(randi(4),:); % choose random direction
curLoc = mod(curLoc + direction - 1, sz) + 1; % (wrap edges)
end
numCells = numCells + 1;
plate(curLoc(1),curLoc(2)) = oldGen;
cellLocs(numCells,:) = curLoc;
end
if numCells >= maxCells
break
end
set(im, 'CData', plate);
pause(.0001)
end
end
fprintf('Max number of cells reached in %d generations\n', generation)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment