저는 matlab를 처음 사용하고 게임을 코딩하려고합니다. 그러나 나는 약간의 어려움을 겪고 있습니다. 모든 셀은 0 또는 1의 값을 가질 수 있습니다. (파이썬 에서처럼 익숙한 유일한 프로그램 인) 카운터를 사용하려하지만 작동하지 않는 것 같습니다. 문제는 모든 세포, 경계 세포에 대해서도 작동해야한다는 것입니다. 내가 각 셀의 값을 가지고 있다면 (그래서 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);
코드가 상당히 길기 때문에 문제가되는 부분과 그 문제가 정확히 무엇인지 설명 할 수 있습니까? 예를 들어 어떤 오류 메시지가 나타 납니까? –
@DedekMraz 찾기. 나는 확인하기 위해 귀찮게하지 않았다. (받아 들여진 대답없이 중복을 막으려 고 내 대답을 제거했다.) –
@EitanT 마스크 매트릭스가 익숙한 모양입니다. 사실 다른 접근법에서 비슷한 접근법 (그리고 가면)을 발견했다. –