我是matlab新手,試圖編寫生活遊戲。但是,我有一些困難,造成了neigbors的總和。每個單元格可以有0或1的值。我正在嘗試使用一個計數器(就像在Python中,這是我所熟悉的唯一程序),但似乎並不奏效。問題是必須適用於所有單元格,所以邊界單元格也適用。如果我有每個細胞的價值(所以這將介於0和8之間),我必須執行規則。但我也不知道這是否正確。請幫忙!我越來越絕望了!如何總結matlab中neigburs的價值?
TIME = 50;
pulsar;
life = {}; % create list 'life'
life{1} = X; % add seed to 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're starting off with in each step)
for i = 0:TIME; % determine amount of times the loop will run
for row = 1:numrows; % for each row
for column = 1:numcolumns; % for each column
if column + 1 ~= numcolumns + 1
east_of_row = column + 1; % define how to count the cell right of target cell
end
if column - 1 ~= 0
west_of_row = column - 1; % define how to count the cell left of target cell
end
if row - 1 ~= 0
north_of_column = row - 1; % define how to count the cell north of target cell
end
if row + 1 ~= numrows + 1
south_of_column = row + 1;
end
neighbors = 0 % start counter 'neighbors' with 0
if current(row,east_of_row) == 1; % if neighboring cell has a value of 1
neighbors = neighbors + 1; % add 1 to neighbors
end
if current(row,west_of_row) == 1; % if neighboring cell has a value of 1
neighbors = neighbors + 1; % add 1 to neighbors
end
if current(north_of_column,column) == 1; % if neighboring cell has a value of 1
neighbors = neighbors + 1; % add 1 to neighbors
end
if current(south_of_column,column) == 1; % if neighboring cell has a value of 1
neighbors = 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 = 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 = 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 = 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 = neighbors + 1; % add 1 to neighbors
end
% 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 % 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
nextnext(row,column) = 1;
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} = nextnext; % add matrix to list 'life
end
show(life);
您的代碼很長,您能否更詳細地解釋什麼部分提出了問題,以及這個問題到底是什麼?例如,你是否收到任何錯誤消息? – 2013-03-13 15:38:01
@DedekMraz很好找。我沒有打擾檢查(刪除我的答案,勸阻重複沒有接受的答案)。 – 2013-03-13 15:54:31
@EitanT你的面具矩陣看起來很熟悉。實際上在另一個問題中找到了類似的方法(和麪具)。 – 2013-03-13 16:08:27