# I'm writing an RREF function of my own and need to perform row swaps to obtain upper triangular form. I have the following augmented matrix: [1,-1,2,0,​7;0,0,1,2,​1;0,0,-1,-​2,-3;0,0.2​,0,0.6,0.6​]. Will you please look at my flawed script? Thanks!

20 views (last 30 days)
Jenel Darland on 27 Apr 2017
Answered: Kevin Gleason on 5 May 2017
%Index columns, then rows, to check if there are any nonzero entries below the matrix(j,j) value
for j=2:n-1
for i=j:m
if i~=j
if matrix(i,j)~=0
%If nonzero entries exist, save the row
temp_row=matrix(i,:);
%Save the column
temp_col=matrix(:,j);
%If sum for specific entries for this column of the matrix=0, check that the sum of entries to the left of matrix(j,j)=0
if sum(matrix(j,j:i-1)<threshold
new_temp_row=matrix(i,1:j);
if sum(new_temp_row(:))<threshold
%If so, swap rows
matrix(i,:)=matrix(j,:);
matrix(j,:)=temp_row;
end
end
end
end
end
end
This code doesn't work properly; in addition, it seems very specific to what I'm trying to accomplish. Is there an easier way to sort through the rows to obtain upper triangular form while keeping the rows intact? The script I wrote transformed the matrix [1,-1,2,0,7;2,-2,2,-4,12;-1,1,-1,2,-4;-3,1,-8,-10,-29] into the above-referenced matrix. I now need to somehow swap rows or sort in order to obtain upper triangular form, and this is where I've tried various methods but am stuck. Thank you.

Kevin Gleason on 5 May 2017
MATLAB does come with an " rref " function. I understand that you want to write yours on your own, but it won't hurt to look in the contents of that file for reference:
>> edit rref
This will open the "rref" file so you can see the implementation.