function z = gooddet(A) % GOODDET z = gooddet(A) % Compute determinant by LU decomposition (Gauss elimination). % O(n^3) work instead of O(n!) like baddet.m % Essentially, this is the built-in method for determinant. n = size(A,2); % get number of columns if n == 1, z = A(1,1); return, end % finish easy case of 1x1 matrix [L,U,P] = lu(A); s = detperm(P); % replacement "s = det(P);" is fine, though "cheating" z = s * prod(diag(U));