如何总结matlab中neigburs的价值?
问题描述:
我是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);
答
您可以使用conv2
来计算这个值,如下所示:
%First create some example matrix
% (this is a 5 x 5 matrix with 30% 1's, 70% 0's
x = full(ceil(sprand(5,5,0.3)))
%Create your convolution kernal
% This will add up all the values in the 8 elements surrounding the
% central element
k = [1 1 1; 1 0 1; 1 1 1];
%Now do the copnvulution (using the 'same' argument to select an output
%the same size as the input.)
counts = conv2(x,k,'same')
这将每次产生轻微不同的例子。但是对于上面代码的执行,我得到以下内容:
x =
0 1 0 1 0
0 1 0 1 0
0 0 1 1 0
0 1 0 1 0
0 0 0 0 0
counts =
2 1 4 1 2
2 2 6 3 3
2 3 5 3 3
1 1 4 2 2
1 1 2 1 1
+0
我应该只使用从我老师那里得到的poiwerpoints的基本操作,conv2()不在那里。 – user2162627 2013-03-13 17:40:56
您的代码很长,您能否更详细地解释什么部分提出了问题,以及这个问题到底是什么?例如,你是否收到任何错误消息? – 2013-03-13 15:38:01
@DedekMraz很好找。我没有打扰检查(删除我的答案,劝阻重复没有接受的答案)。 – 2013-03-13 15:54:31
@EitanT你的面具矩阵看起来很熟悉。实际上在另一个问题中找到了类似的方法(和面具)。 – 2013-03-13 16:08:27