2017-11-10 1 views
0

나는 Conway의 Game of Life의 초기 설정을 어지럽 혔고 몇 가지 문제가 있습니다. 나는 살아있는 이웃 입자 (이 'positionSum'이라고 부름)의 수가 정확히 계산되지 않은 이유를 알아낼 수 없다. 다음 MATLAB 코드가 있습니다.Conway의 삶의 게임 부정확 한 이웃 수

간단한 코드로 작업하기 위해 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) 

이 프로세스를 사용하면 잘못된 총계가 발생한다고 생각합니다. 나는 다음과 같은 수를 얻을 수 (here를 볼 때) 왼쪽 상단에 흰색과 3 × 3 바둑판를 들어

:

4 5 4 
5 4 5 
4 5 4 
+0

[this] (https://stackoverflow.com/a/3514906/52738) 여기에 연결하는 것으로 생각했습니다. 재미있을 수도 있습니다. – gnovice

답변

0

난 당신이, southeast, north에 대한 그 배열을 선택한 이유를 모르겠어요 또는 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 
관련 문제