2017-11-10 2 views
0

私はConwayのGame of Lifeの初期化をめちゃくちゃにしていて、いくつかの問題にぶち当たっています。私は私の人生のためになぜ生きている隣人の粒子(私は 'positionSum'と呼んでいる)の数が正しく数えられていないのか理解できません。私は以下のMATLABコードを持っています。ConwayのGame of Life不正確な隣人数

私は簡単な3x3グリッドから作業を開始しています。

R = 3; C = 3; % row and column numbers 

X = rand(R, C); % generates random grid 
Y = X < 0.5; % creates array of logicals 
A = Y; 

imshow(Y, 'InitialMagnification', 'fit') % shows initial cell configuration 

north = [R, 1:R-1]; % north neighbour 
east = [2:C, 1];  % east neighbour 
south = [2:R, 1]; % south neighbour 
west = [C, 1:C-1]; % west neighbour 

% gives the total number of live neighbours 
positionSum = A(north, :) + A(south, :) + A(:, east) + A(:, west) ... 
+ A(north, east) + A(north, west) + A(south, east) + A(south, west) 

このプロセスを使用すると、不正確な合計が発生していると思います。左上の白と3x3のチェッカーボードの場合

here見られるように)私は、次のカウントを取得する:

4 5 4 
5 4 5 
4 5 4 
+0

[this](https://stackoverflow.com/a/3514906/52738)とここでリンクしていると思っていました。興味深いかもしれません。 – gnovice

答えて

0

あなたがnortheastsouthためにそれらの配列を選択した理由は、私はわからないんだけど、またはwest。より簡単な方法は、マトリックスの境界線をゼロパッドし、シフトしたバージョンを追加することです。

A = randi([0,1], 3, 3); % Initialise random 0/1 matrix 
% Timestep loop for Game of Life 
numsteps = 10; 
for ii = 1:numsteps 
    % Create total matrix which has a border of 0s compared to A 
    % Meaning it's 2 bigger in each dimension 
    temp = zeros(size(A)+2); 
    % Add in 4 directions. Middle A-sized region of temp is temp(2:end-1,2:end-1) 
    temp(1:end-2, 2:end-1) = temp(1:end-2, 2:end-1) + A; % Shift A up 
    temp(3:end, 2:end-1) = temp(3:end, 2:end-1) + A; % Shift A down 
    temp(2:end-1, 1:end-2) = temp(2:end-1, 1:end-2) + A; % Shift A left 
    temp(2:end-1, 3:end) = temp(2:end-1, 3:end) + A; % Shift A right 
    % Continue for diagonal neighbours 
    % temp(... 

    % Extract number of neighbours from middle region of temp 
    neighbs = temp(2:end-1, 2:end-1); 
    % Continue with chosen GoL rules now we have neighbours 
    % ... 
    % A = ... 
end 
関連する問題