Adding counter to count the number of row swaps for GEA
4 views (last 30 days)
I want to add a counter to a function to count the number of row interchanges for a determinant calculation but it isnt incrementing.
Im getting the right answer for the determinant but only 0 for the count.
here is my code and thanks for your guidance:
function Det = Det_with_RI(A)
% This function calculates determinant of a square matrix A.
% Input : A -- a square matrix.
% Output : Det -- determiant of A.
Det = 1; % Initial value of determinant: expecting to be changed.
Count = 0; % inital value for number of row swaps
% We use here a variable ii in the following for-cycle,
% unlike the usual i variable, since a child-function
% sees parent-function's variables and changes them.
if A(ii,k)~=0, % If a(i,k) is non-zero,
cc=A(ii,:); % swap row i and row k.
Count = Count + 1; % Increment the row swap counter
Det = Det*(-1);
error('The system is indeterminate.');
[m,n] = size(A);
if m~=n error('The argument must be a square matrix.'); end;
% Forward Phase of GEA:
for k = 1:n-1
for i = k+1:n
% Pivoting multiplier:
m = A(i,k)/A(k,k);
% Now we apply Elementary Row Operation of type~I.
% As is known, ERO's of type~I do not change determinant.
% Now, A is an upper triangular matrix.
% In calculation of determinant the Backward Phase is not necessary.
% It is because determinant of an upper triangular matrix
% is the product of its diagonal entries, which we calculate here:
for k=1:n, Det=Det*A(k,k);
Shravan Kumar Vankaramoni on 12 Oct 2021
The above line looks suspicious to me. What is the range of values you have in your input matrix (A). Because the above line is checking whether the value at A(k,k) is less than 10^(-10) i.e 0.0000000001. So if you have values less than 10^(-10), count will be incremented otherwise count is 0.