function C = cofactorMatrix(M)
dim = length(M);

C = zeros(dim);
for i = 1:dim
    for j = 1:dim
        minor = minorMatrix(M,i,j);
        C(i,j) = (-1)^(i+j)*determinant(minor);
    end
end