Obi - presumably your matrix is RxCx3 and that red is (255,0,0), green is (0,255,0), blue is (0,0,255), and white is (255,255,255)..or perhaps you have some other colour scheme that will allow you to distinguish red from blue from green, etc. You can define each rule as a function and you can prioritize the rules, applying each function to your cell until one rule is satisfied (presuably once this happens, you don't need to apply any more rules). Each function would have the same signature
function [updatedCellColour] = applyRule1(gameBoard, cellCoordinates)
where the inputs are the RxC array (the current state of the game board on iteration x), and the cell (x,y) coordinates. The output is the colour of the cell - if it is empty, then the rule could not be applied and so you would move on to the next rule.
I suppose your code could look something like
function [gameBoard] = myCellularAutomata(gameBoard)
[R, C, ~] = size(gameBoard);
for k = 1:1000
tempGameBoard = zeros(size(gameBoard));
updatedCellColour = [];
for r = 1:R
for c = 1:C
[updatedCellColour] = applyRule1(gameBoard, [r c]);
if isempty(updatedCellColour)
[updatedCellColour] = applyRule2(gameBoard, [r c]);
if isempty(updatedCellColour)
end
end
if isempty(updatedCellColour)
tempGameBoard(r,c,:) = updatedCellColour;
end
end
end
gameBoard = tempGameBoard;
end
end
In the above "rough" code, you pass in the game board that has already been initialized with the first set of colours. We iterate for a number of generations/iterations (in this case, 1000) and on each iteartion, we look at each cell and apply the set of rules. if one rule does not satisfy the condition(s) then we try the next rule. However if the rule could not be applied, we move on to the next one. If no rule is satisfied, then there is no change to the cell. If one rule is satisfied, then we save the colour to a temporary game board (which is really the updated game board for that iteration). Once we have iterated over all cells, then we save the temp game board and move to the next iteration.
Out of curiosity, how is your second rule to be interpreted? If a cell has exactly one green cell next to it (what does next mean - can it be any one of the up to eight neighbours?) do we make sure that there is no change to the cell? What if it has two green cells? Or four? i guess you have to be very explicit with how you define each rule...