
% 'final' stores final state of the board
function final = final_state_3(b,x)
	% numbering the buttons
	a = [1, 2, 3; 4, 5, 6; 7, 8, 9];

	% A(i, j) = 1 if button i is toggled on pressing button j
	A = zeros(9,9);

	% initializing A 
	for i = 1 : 3
		for j = 1 : 3
			A(a(i,j), a(i,j)) = 1;
			if (i + 1 <= 3)
				A(a(i,j), a(i + 1, j)) = 1;
			end
			if (i - 1 >= 1)
				A(a(i,j), a(i - 1, j)) = 1;
			end
			if (j + 1 <= 3)
				A(a(i,j), a(i, j + 1)) = 1;
			end
			if (j - 1 >= 1)
				A(a(i,j), a(i, j - 1)) = 1;
			end
		endfor
	endfor

	% b - initial state of board
	% x - action vector
	% reshaping x and b from 3x3 to 9x1
	x = reshape(x, 9, 1);
	b = reshape(b, 9, 1);

	final = reshape(mod((A*x + b), 2), 3, 3);
endfunction

