2021-03-13 16:16:01 +01:00
|
|
|
function A = Alg6_RREF(A)
|
2021-03-13 20:23:23 +01:00
|
|
|
% Algorithm 6: Reduced Row Echelon Form (RREF)
|
2021-03-13 17:46:26 +01:00
|
|
|
% A = Alg6_RREF(A) returns RREF of matrix A.
|
2021-03-13 16:16:01 +01:00
|
|
|
|
|
|
|
[M, N] = size(A);
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
for m = 1 : M
|
2021-03-13 17:46:26 +01:00
|
|
|
n = n + 1;
|
2021-03-13 16:16:01 +01:00
|
|
|
if n > N
|
|
|
|
break
|
|
|
|
end
|
|
|
|
% We want the left-most coefficient to be 1 (pivot)
|
|
|
|
row = A(m, :);
|
|
|
|
if row(m) == 0
|
2021-03-13 17:46:26 +01:00
|
|
|
n = n + 1;
|
2021-03-13 16:16:01 +01:00
|
|
|
end
|
|
|
|
row = row/row(n);
|
|
|
|
A(m, :) = row;
|
|
|
|
|
|
|
|
for i = 1 : M
|
|
|
|
if i ~= m
|
|
|
|
A(i, :) = A(i, :)-(A(i, n))*row;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for i = m + 1 : M
|
|
|
|
A(i:end, m+1:end); % Partial matrix (in which we are looking for non-zero pivots)
|
|
|
|
A(i:end, m+1); % Left-most column
|
|
|
|
if ~any(A(i:end, m+1)) % If the left-most column has only zeros check the next one
|
|
|
|
m = m + 1;
|
|
|
|
end
|
|
|
|
A(i:end, m+1:end);
|
|
|
|
if A(i, m+1) == 0
|
|
|
|
non_zero_row = find(A(i:end,m+1), 1);
|
|
|
|
if isempty(non_zero_row)
|
|
|
|
continue
|
|
|
|
end
|
|
|
|
A([i, i+non_zero_row-1], :) = deal(A([i+non_zero_row-1, i], :));
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|