随机数生成 - 感染

时间:2018-03-19 15:22:31

标签: matlab

InfectionMatrix = zeros(100,100);
InfectionMatrix(50,50) = 1;


    for k = 1:1:90

        [i,j] = find(InfectionMatrix > 0.7);



        %Random Number Generator
        a = 0.0270;
        b = 0.3250;
        RANDOM = (b-a).*rand(8,1) + a;
        %%%%%%%%%%%%%%%%%%%%%%%%



       i(i == 1) = [];
       i(i == length(InfectionMatrix)) = [];
       j(j == 1) = [];
       j(j == length(InfectionMatrix)) = [];



                        InfectionMatrix(i-1,j) = InfectionMatrix(i-1,j)+      RANDOM(1); 
                        InfectionMatrix(i-1,j+1) = InfectionMatrix(i-1,j+1)+  RANDOM(2);
                        InfectionMatrix(i-1,j-1) = InfectionMatrix(i-1,j-1)+  RANDOM(3); 
                        InfectionMatrix(i,j+1) = InfectionMatrix(i,j+1)+      RANDOM(4); 
                        InfectionMatrix(i,j-1) = InfectionMatrix(i,j-1)+      RANDOM(5);
                        InfectionMatrix(i+1,j+1) = InfectionMatrix(i+1,j+1) + RANDOM(6); 
                        InfectionMatrix(i+1,j-1) = InfectionMatrix(i+1,j-1) + RANDOM(7);
                        InfectionMatrix(i+1,j) = InfectionMatrix(i+1,j) +     RANDOM(8); 

       rng('shuffle')

     TooHigh = find(InfectionMatrix > 100);

                  for m = 1:1:length(TooHigh)                      
                InfectionMatrix(TooHigh(m)) = 100;
                 end
    end
h = heatmap(InfectionMatrix)
old_warning_state = warning('off', 'MATLAB:structOnObject');
hs = struct(h);
warning(old_warning_state);

hs.XAxis.TickValues = [];
hs.YAxis.TickValues = [];

我想模仿一种传播感染的简单观点。有两件事我想完成。

我想知道如何将感染的传播更改为更圆形,明显存在角节点以星形进展的问题。

我也想知道如何将地图的下半部分改为顶部的一半,即随机数生成RANDOM将为RANDOM/2 那种想法。

我很感激任何改进我的代码的帮助,这些帮助与我指定的问题无关。

1 个答案:

答案 0 :(得分:0)

我不确定你的目标是什么,但我认为你会想要使用这种类型的2d卷积

Infected = zeros(100);
Infected(50,50) = 1;
InfectionMatrix = zeros(100);

for k = 1:90
    RANDOM = (b-a).*rand(3,3) + a;
    RANDOM(2,2) = 0;       
    RANDOM = RANDOM .* fspecial('gaussian',3,1);  % sigma=1 3x3 kernel
    InfectionMatrix = InfectionMatrix + conv2(Infected,RANDOM,'same');
    Infected = InfectionMatrix > 0.7;
end

您可以通过高斯滤波器调制随机数,例如fspecial(),以确保它们在角落处脱落。