%A = floor(2*rand(3,3)); % make an abritrary input
%disp('The matrix to be solved is:')
%disp(A); % print it

function [answer] = lookup_solve_3(A)  		% takes A as an arguement

lookup_matrix = [3 7 4 6 5 1 2];		% decimal form of the given table

answer = zeros(3);                              % the required final action matrix

dec_equiv = 1;                                  % to enter the below loop for first time

while dec_equiv > 0                             % checks decimal equivalent of the last row
  for variable = [2 3]                          
    action = zeros(3);                          % action matrix : 0 means no flipping of switch
    action(variable,:) = A(variable-1,:);       % ith row of action matrix is updated with the (i-1)th row of input A
    A = final_state_3(A,action);
    answer = answer+action;                     % each action matrix is added to this final answer matrix
  end
  
  dec_equiv = 4*A(3,1)+2*A(3,2)+A(3,3);         % calculates decimal equivalent of the last binary row. 
  if(dec_equiv>0)
    num = lookup_matrix(dec_equiv);	        % num is the corresponding decimal from the lookup_matrix 
    C = zeros(3,1);                             
    
    for var = [3 2 1]                           % breaking num into binary form and storing it in the row vector C 
      C(var) = mod(num,2);
      num = floor(num/2);
    end
    
    %change A/
    action1 = zeros(3);				
    action1(1,:) = C;				% action to be taken in the first row
    A = final_state_3(A,action1);			% state of A after action1
    answer = answer+action1;                    % updating the final answer matrix
    answer = mod(answer,2);                   
  endif
endwhile

answer = mod(answer,2);

%disp('The answer is :')
%disp(answer);
return 
endfunction
