% Amey Kulkarni
% Gauss Jordan (With Partial Pivot) Matrix Inversion Algorithm 
% This code was written to understand the matrix inversion by using
% Gauss Jordan algorithm
% Please do not misuse the code ( i.e. as a Home work solution) 
clc;
clear all;
mat_size=length(a);
%a=[4 1 13;10 3 2;3 12 1];
a_temp=a;
b=eye(mat_size);
for i=1:mat_size
    col=a(i:end,i);
    [pivot,maxrow_temp]=max(col);
    max_row=maxrow_temp+i-1;
    a([i max_row],:) = a([max_row i],:);
    b([i max_row],:) = b([max_row i],:);

    for j=i+1:mat_size
         mult=a(j,i)/a(i,i);
         row_temp_a=a(i,:)*mult;
         row_temp_b=b(i,:)*mult;
         a(j,:)=a(j,:)-row_temp_a;
         b(j,:)=b(j,:)-row_temp_b;
    end
end


for i=1:mat_size
     for j=1:i-1
         mult=a(j,i)/a(i,i);
         row_temp_a=a(i,:)*mult;
         row_temp_b=b(i,:)*mult;
         a(j,:)=a(j,:)-row_temp_a;
         b(j,:)=b(j,:)-row_temp_b;
     end
end
 
 for i=1:mat_size
    temp=a(i,i);
    a(i,:) = a(i,:)/temp;
    b(i,:) = b(i,:)/temp;
 end
 disp('Matrix A turn in to Identity Matrix');disp(a);
 disp('Matrix B turn in to Inverse of "A" Matrix');disp(b);
 disp('Cross Comparison with results of "INV"function of matlab'); d=inv(a_temp);disp(d);
