2013-03-12 4 views
0

저는 Matlab에서 Conway의 삶의 게임을 코딩하려고 노력하고 있습니다.하지만 뭔가 잘못 될 수 있습니다. 나는 내가 잘못한 것을 정말로 알지 못하므로 오류가 발생하지 않으며 단지 아무 것도하지 않습니다. 나는이 문제가 세포 수 계산 (이웃 수 확인)과 행렬의 경계에서 세포에 대한 규칙을 증가시키는 것과 관련이 있다고 생각한다.Matlab에서의이 삶의 게임은 무엇이 문제입니까?

TIME = 10; 
pulsar;  % creates begin matrix X 
life{1} = X; 
life = {};   % create list 'life' 

numrows = size(X,1); % calculate number of rows 
numcolumns = size(X,2); % calculate number of columns 

current = X;  % make seed the first current(matrix you are starting off with in each step) 
for i = 0:TIME; % determine amount of times the loop will run 
    nextnext = X;      % create "nextnext" matrix to implement the rules of the game on (copy of X) 
    for row = 2:numrows-1;    % for each row 
     for column = 2:numcolumns-1; % for each column 
      east_of_row = column + 1;  % define how to count the cell right of target cell 
      west_of_row = column - 1;  % define how to count the cell left of target cell    
      north_of_column = row - 1;  % define how to count the cell north of target cell    
      south_of_column = row + 1;  % define how to count the cell south of target cell    

      % count neighboring cells: 
      neighbors = 0;        % start counter 'neighbors' with 0 

      while east_of_row <= size(X), 
       west_of_row <= size(X);, 
       north_of_column <= size(X);, 
       south_of_column <= size(X); 
       if current(row,east_of_row) == 1    % if neighboring cell has a value of 1 
        neighbors + 1;         % add 1 to neighbors 
       end 
       if current(row,west_of_row) == 1    % if neighboring cell has a value of 1 
        neighbors + 1;         % add 1 to neighbors 
       end 
       if current(north_of_column,column) == 1  % if neighboring cell has a value of 1  
        neighbors + 1;         % add 1 to neighbors 
       end 
       if current(south_of_column,column) == 1  % if neighboring cell has a value of 1  
        neighbors + 1;         % add 1 to neighbors 
       end 
       if current(south_of_column,east_of_row) == 1 % if neighboring cell has a value of 1 
        neighbors + 1;         % add 1 to neighbors 
       end 
       if current(north_of_column,east_of_row) == 1 % if neighboring cell has a value of 1 
        neighbors + 1;         % add 1 to neighbors 
       end 
       if current(north_of_column,west_of_row) == 1 % if neighboring cell has a value of 1 
        neighbors + 1;         % add 1 to neighbors 
       end 
       if current(south_of_column,west_of_row) == 1 % if neighboring cell has a value of 1 
        neighbors + 1;         % add 1 to neighbors  
       end 
      end 

    while east_of_row == 0,west_of_row == 0;,north_of_column == 0;,south_of_column == 0; 
    if current row,east_of_row ~= 0; 
    if current(row,east_of_row) == 1    % if neighboring cell has a value of 1 
     neighbors + 1;         % add 1 to neighbors 
    end 
    end 
    if current row,west_of_row ~= 0; 
    if current(row,west_of_row) == 1    % if neighboring cell has a value of 1 
     neighbors + 1;         % add 1 to neighbors 
    end 
    end 
    if current north_of_column,column ~= 0; 
    if current(north_of_column,column) == 1  % if neighboring cell has a value of 1 
     neighbors + 1;         % add 1 to neighbors 
    end 
    end 
    if current south_of_column,column ~= 0; 
    if current(south_of_column,column) == 1  % if neighboring cell has a value of 1 
     neighbors + 1;         % add 1 to neighbors 
    end 
    end 
    if current south_of_column,east_of_row ~= 0; 
    if current(south_of_column,east_of_row) == 1 % if neighboring cell has a value of 1 
     neighbors + 1;         % add 1 to neighbors 
    end 
    end 
    if current north_of_column,east_of_row ~= 0; 
    if current(north_of_column,east_of_row) == 1 % if neighboring cell has a value of 1 
     neighbors + 1;         % add 1 to neighbors 
    end 
    end 
    if current north_of_column,west_of_row ~= 0; 
    if current(north_of_column,west_of_row) == 1 % if neighboring cell has a value of 1 
     neighbors + 1;         % add 1 to neighbors 
    end 
    end 
    if current south_of_column,west_of_row ~= 0; 
    if current(south_of_column,west_of_row) == 1 % if neighboring cell has a value of 1 
     neighbors + 1;         % add 1 to neighbors  
    end 
    end 
    end 
    neigbors 

     % rules of the game: 

    if current(row,column) == 1    % in case a target cell has a value of 1: 

     if neighbors < 2       % if the number of neighbors is smaller than 2 
      nextnext(row,column) = 0;     % value of target cell gets 0 in nextnext 
     end 
     if neighbors == 2 , neighbors == 3  % if the number of neighbors is 2 or 3 
      nextnext(row,column) = 1;     % value of target cell stays 1 in nextnext 
     end 
     if neighbors > 3      % if the number of neigbors is higher than 3 
      nextnext(row,column) = 0;     % value of target cell gets 0 in nextnext 
     end 
    end 
    if current (row,column) == 0   % in case a target cell has a value of 0: 

     if neighbors == 3       % if the number of neighbors is 3 
      nextnext(row,column) = 1;     % value of target cell gets 1 in nextnext 
     end 
     if neighbors ~= 3      % if the number of neigbors isn't 3 
      nextnext(row,column) = 0;     % value of target cell stays 0 in nextnext 
     end 
end 
    end 
    end 



current = nextnext;  % make nextnext matrix the current matrix for the next step 
life{TIME+1} = nextnext; % add matrix to list 'life 
end 


show(life); 

답변

1

와우 :

여기 내 코드입니다. 귀하의 코드는 여러면에서 좋지 않습니다 ... 당신은 matlab에 vectorization 들어 보셨나요? 당신이 이웃의 수를 일단

neighbors = conv2(current, [1 1 1;1 0 1; 1 1 1], 'same'); 

당신이 단순히 다음 단계

nextnext = current.*(neighbors == 2 | neighbors == 3) + ... % all cells for which current == 1 
      (1 - current).*(neighbors == 3); 

일부를 만들 수 있습니다

각 셀은 단순히 그것을 할 수있다 이웃의 수를 계산하려면 가지고있는 코드의 문제점 :

  1. 라인 neighbors + 1; % add 1 to neighbors 실제로 d oes 아니요 이웃에 하나를 더합니다. Matlab에는 neighbors++에 해당하는 것이 없습니다. 당신이 정말로 neighbors를 증가 싶다면, 당신은 명시 적으로해야 할 것이다 : 당신이 while 루프하지만 문 while east_of_row == 0,west_of_row == 0;,north_of_column == 0;,south_of_column == 0;를 사용하는 이유 나도 몰라 neighbors = neighbors + 1;

  2. 만 첫 번째 조건 east_of_row == 0에게 표현으로 평가 나머지 부분을 확인 루프 조건으로 간주되지 않습니다.

  3. 다음에 예상 한 것을 얻지 못하고 오류가 표시되지 않으면 코드를 단계별로 debugging 번 시도하십시오.

BTW, 당신은

>> life 
+5

당신이 생명의 tweetable MATLAB 게임을 본 적이 시도? 's = [1 1 1] '; n = @ (a) conv2 (s, s ', 1 * a,'same ') - a; lf = @ (a) n (a) == 2 & a | n (a) == 3; a = 랜드 (128)> 0.8; ii = 1 : 500, spy (a); drawnow; a = lf (a); end'에 대한 그래픽도 포함됩니다! Matt McDonnell에게 감사드립니다. –

+0

@SamRoberts - 매우 좋음 :-) – Shai

+1

@SamRoberts 어때 [this one] (http://stackoverflow.com/a/3514906/52738)? ;) – gnovice

관련 문제