2013-03-12 20 views
0

我正在嘗試在Matlab中對康威的生活遊戲進行編碼,但是一直在出錯。我沒有得到一個錯誤,所以我不知道我做錯了什麼,它只是沒有做任何事情。我認爲這個問題可能與計算單元格有關(檢查鄰居的數量)或增加矩陣邊界單元格上的規則。這個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中的矢量化?

要計算鄰居的數量每個單元有你可以簡單地做到這一點:

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你必須明確地做到這一點:neighbors = neighbors + 1;

  2. 我不知道爲什麼你使用while循環但聲明while east_of_row == 0,west_of_row == 0;,north_of_column == 0;,south_of_column == 0;只檢查第一個條件east_of_row == 0其餘爲表達式求值並不算作循環條件。

  3. 下一次,如果您沒有得到您所期望的並且沒有看到任何錯誤,請嘗試debugging您的代碼一步一步。

順便說一句,你有沒有試過

>> life 
+5

你見過生活的tweetable MATLAB遊戲? 's = [1 1 1]'; n = @(a)conv2(s,s',1 * a,'same') - a; (a)n(a)== 2&a | n(a)== 3; a = rand(128)> 0.8; for ii = 1:500,spy(a); drawnow; a = lf(a); end'它甚至包含圖形!感謝Matt McDonnell。 – 2013-03-13 01:21:15

+0

@SamRoberts - 非常好:-) – Shai 2013-03-13 06:37:17

+1

@SamRoberts [this one](http://stackoverflow.com/a/3514906/52738)? ;) – gnovice 2013-12-11 20:20:14