function x = mydet( A )
%MYDET calculates determinant by expansion on first row
%       we will assume (without checking) that A is square  
%       if A is not square, this function will not work
%
% The function works by calling itself over and over again
% on the minors of the matrix.   Eventually the minor should
% have only one element.  
%
%  NOTE: this will require multiplying n elements n! times.
%   (n-1)*n! multiplications takes a long time, even for a 
%   computer.

x = 0;            % x will be the calculated determinant
[numrows,numcols] = size(A);  

if (numrows == 1)   % if the matrix has only one entry,  
    x = A(1,1);     %  then that entry is the determinant 
else                % otherwise 
    for k=1:numcols %  for each element of the first row 
                    %  add A(1,k) times det( 1,k - minor ) 
        x = x + (-1)^(1+k)*A(1,k)*mydet(A( 2:m , [1:(k-1) (k+1):n] ));
    end
end
end

