2013-03-13 2 views
0

저는 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); 
+0

코드가 상당히 길기 때문에 문제가되는 부분과 그 문제가 정확히 무엇인지 설명 할 수 있습니까? 예를 들어 어떤 오류 메시지가 나타 납니까? –

+0

@DedekMraz 찾기. 나는 확인하기 위해 귀찮게하지 않았다. (받아 들여진 대답없이 중복을 막으려 고 내 대답을 제거했다.) –

+0

@EitanT 마스크 매트릭스가 익숙한 모양입니다. 사실 다른 접근법에서 비슷한 접근법 (그리고 가면)을 발견했다. –

답변

2

다음과 같이,이 값을 계산하는 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