问题描述
我正在尝试在Matlab中编写Conway的生活游戏,但是某些地方总是出错.我没有收到错误,所以我真的不知道自己在做什么错,只是什么也没做.我认为问题可能与细胞计数(检查邻居数)或增加矩阵边界处的细胞规则有关.
I'm trying to code Conway's game of life in Matlab but something keeps going wrong. I don't get an error so I don't really know what I'm doing wrong, it just doesn't do anything. I think the problem might have something to do with the counting of the cells (to check the number of neighbors) or increment the rules on the cells at the border of the matrix.
这是我的代码:
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);
推荐答案
哇.您的代码在很多方面都是不好的...您听说过matlab中的向量化吗?
Wow. your code is bad in so many ways... Have you heard of vectorization in matlab?
要计算每个像元的邻居数,您可以通过以下方式简单地做到这一点:
To count the number of neighbors each cell has you can do it simply by:
neighbors = conv2( current, [1 1 1;1 0 1; 1 1 1], 'same' );
一旦邻居数量众多,您就可以简单地创建下一个步骤
Once you have the number of neighbors you can simply create the next time step
nextnext = current.*( neighbors == 2 | neighbors == 3 ) + ... % all cells for which current == 1
( 1 - current ).*( neighbors == 3 );
您的代码存在一些问题:
Some problems with the code you have:
-
neighbors + 1; % add 1 to neighbors
行实际上没有不添加一个到邻居. Matlab没有等效的neighbors++
.因此,如果您真的想增加neighbors
,则必须明确地执行:neighbors = neighbors + 1;
The line
neighbors + 1; % add 1 to neighbors
actually does not add one to neighbor. Matlab does not have the equivalent ofneighbors++
. So, if you really want to incrementneighbors
you'll have to do it explicitly:neighbors = neighbors + 1;
我不知道您为什么使用while
循环,但是语句while east_of_row == 0,west_of_row == 0;,north_of_column == 0;,south_of_column == 0;
仅检查第一个条件east_of_row == 0
,其余部分作为表达式求值,不计为循环条件.
I don't know why you use while
loop but the statement while east_of_row == 0,west_of_row == 0;,north_of_column == 0;,south_of_column == 0;
only checks the first condition east_of_row == 0
the rest are evaluated as expressions and do not count as loop conditions.
下次,如果您没有得到期望的结果并且没有看到任何错误,请尝试逐步调试代码.
Next time, if you don't get what you expect and you don't see any error, try debugging your code step by step.
顺便说一句,你尝试过吗
BTW, have you tried
>> life
这篇关于Matlab中的这种生活游戏有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!