%MINOR and COFACTOR FOR THE ij^th ELEMENT
%(Selcuk Bayin, METU-NCC, bayin@metu.edu.tr)
clc
n=5;
A=rand(n)
%%
%for i=5,j=3;
%Submatrix A(i|j)
SubMatrix=A([1:i-1 i+1:n],[1:j-1 j+1:n])
%%
%(minor)ij=determinant of the Submatrix A(i|j)
minorAij=det(A([1:i-1 i+1:n],[1:j-1 j+1:n]))
%%
%Cofactor is the signed minor
cofactorAij=(-1)^(i+j)*det(A([1:i-1 i+1:n],[1:j-1 j+1:n]))
%COFACTOR MATRIX A
%%
n=6
A=rand(n)
for i=1:n
    for j=1:n
    cofactorA(i,j)=(-1)^(i+j)*det(A([1:i-1 i+1:n],[1:j-1 j+1:n]));
    end
end
cofactorA
%%
adjA=cofactorA'
Ainv=adjA/det(A)
%%
%CHECK THE INVERSE FOUND
A*Ainv
%Inverse by Mathlab
inv(A)
%Compare
Ainv
    