DRG*_*DRG 8 simulation recursion matlab loops while-loop
我有一个5000x5000网格,我正在尝试在MATLAB中实现一个简单的癌症分区模型.最初,它选择一个随机点(x,y)并使该细胞成为癌细胞.在第一次迭代中,它除以 - 父单元停留在其位置,子单元被随机分配给任何相邻单元.
容易到目前为止.
我的问题在于:在连续迭代中,子细胞通常被分配到已经具有癌细胞的细胞.在这种情况下,我希望子单元取代它并将已经存在的单元"撞击"到相邻单元.如果该相邻单元格为空,则将其填充并且该过程停止.如果没有,则已经在该位置的单元格被撞击,依此类推,直到最后一个单元格找到空白空间并且该过程停止.
这应该很简单,但我不知道如何编写它以及使用什么类型的循环.
我是一个物理科学家而不是程序员,所以请像对待傻瓜一样对待我!
这是我编写的一个函数,大致符合您提供的规格。
随着癌细胞数量的增加,我的速度确实变慢了。
基本上我有几个变量,NxN 矩阵代表单元格位置的网格(我称其为 a,plate
因为网格是现有 matlab 函数的名称)
我可以快速迭代的点向量。我选择一个种子位置,然后运行 while 循环,直到网格填满。
在每次循环迭代中,我对每个单元格执行以下操作:
我还没有对其进行广泛的测试,但它似乎有效。
function simulateCancer(plateSize, pDivide)
plate = zeros(plateSize, plateSize);
nCells = 1;
cellLocations = zeros(plateSize*plateSize,2);
initX = randi(plateSize);
initY = randi(plateSize);
cellLocations(nCells,:) = [initX, initY];
plate(initX, initY) = 1;
f = figure;
a = axes('Parent', f);
im = imagesc(plate, 'Parent', a);
while(nCells < (plateSize * plateSize))
currentGeneration = currentGeneration+1;
for i = 1:nCells
divide = rand();
if divide <= pDivide
divideLocation = cellLocations(i,:);
divideDir = randi(4);
[x, y, v] = findNewLocation(divideLocation(1), divideLocation(2), plate, divideDir);
if (v==1)
nCells = nCells+1;
plate(x,y) = 1;
cellLocations(nCells,:) = [x,y];
end
end
end
set(im,'CData', plate);
pause(.1);
end
end
function [x,y, valid] = findNewLocation(xin, yin, plate, direction)
x = xin;
y = yin;
valid = 1;
% keep looking for new spot if current spot is occupied
while( plate(x, y) == 1)
switch direction
case 1 % divide up
y = y-1;
case 2 % divide down
y = y+1;
case 3 % divide left
x = x-1;
case 4 % divide down
x = x+1;
otherwise
warning('Invalid direction')
x = xin;
y = yin;
return;
end
%if there has been a collision with a wall then just quit
if y==0 || y==size(plate,2)+1 || x==0 || x==size(plate,1)+1 % hit the top
x = xin; %return original values to say no division happend
y = yin;
valid = 0;
return;
end
end
end
Run Code Online (Sandbox Code Playgroud)
注意:我没有考虑推送单元格,而是以将单元格保留在当前位置并在行/列末尾创建新单元格的方式进行编码。在语义上它是不同的,但在逻辑上它具有相同的最终结果,只要你不关心代数。