function d=determ(A) % Recursive function to % calculate the determinant of a matrix [m n]=size(A); % Test to see that the matrix is square if m ~= n error(sprintf('Matrix %d x %d is not square.',m,n)); end % This switch statement allows special cases for 1x1 and 2x2 switch n % 1x1 matrix case 1 d=A(1,1); % 2x2 matrix case 2 d=A(1,1)*A(2,2)-A(2,1)*A(1,2); % General case nxn matrix otherwise % Expand on the first column over cofactors d=0; for i=1:n % Loop over first column d = d + ... % Accumulate the sum A(i,1) * ... % i-th element of first column ((-1)^(i-1)) * ... % Sign of cofactor determ( ... % Recursive function call A([1:(i-1) (i+1):end],[2:end])); % Derived cofactor end % end for i=1:n end % end switch n