0

コンウェイの人生ゲームを 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);
4

1 に答える 1

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実際にはネイバーに 1 を追加しません。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. 次回、期待どおりの結果が得られず、エラーが表示されない場合は、コードを段階的にデバッグしてみてください。

ところで、あなたは試しましたか

>> life
于 2013-03-12T20:53:53.950 に答える