Help with function putting a matrix into upper triangluar form.

I have this function but it just is not doing what I want it to. When the function reaches to the end of the second for, it goes through that same for loop over again. How can I fix this? The Largest, swap and matCon are my own functions, they fins the row with the largest value, swap two rows and create a liner combination of the rows.
%%Converts a matrix to upper triangular form
function [upperMat]=upperTriangle(M)
rows=size(M,1);
cols=size(M,2);
for curRow=1:rows-1;
maxRow=Largest(M,curRow,curRow);
if maxRow>curRow;
M=swap(M,maxRow,curRow);
end
for workRow=curRow+1:rows
alpha=-M(workRow,curRow)/M(curRow,curRow);
M=matCon(M,curRow,workRow,alpha);
end
upperMat=M;
end
end

 Accepted Answer

Marc - you probably need to post the code for your three functions as one of them may be causing a problem. If I take your code and use the following substations for your three
function [maxRowIdx] = Largest(M,startRowIdx,colIdx)
[~,maxRowIdx] = max(M(startRowIdx:end,colIdx));
end
function [M] = swap(M,row1Idx,row2Idx)
temp = M(row1Idx,:);
M(row1Idx,:) = M(row2Idx,:);
M(row2Idx,:) = temp;
end
function [M] = matCon(M,row1Idx,row2Idx,alpha)
M(row2Idx,:) = alpha*M(row1Idx,:) + M(row2Idx,:);
end
then the algorithm works. For example
upperTriangle(magic(5))
ans =
23 5 7 14 16
0 20.304 -4.1739 -2.3478 3.1739
0 0 12.837 18.158 18.415
0 0 0 -9.3786 -31.28
0 0 0 0 90.173

5 Comments

Hi Geoff, the functions are as follows
Largest
function maxRow=Largest(M,r,c)
rows=size(M,1); % total number of rows
max=M(r,c);
maxRow=M(r,:);
for i=r:rows
if abs(M(i,c))>max % Checks to see if the absolute value of a column is greater than max
max=abs(M(i,c));
maxRow=i; % updates maxRow
end
end
end
swap
function newmat=swap(mat,row1,row2) % swaps two rows in a matrix
[r, c] = size(mat); % find number of rows and columns
newmat=mat; % interchange values
newmat(row2,:) = mat(row1,:);
newmat(row1,:) = mat(row2,:);
end
matCon
%%takes a matrix, two row numbers and a scalar as arguments and returns a matrix with a linear combination of the rows
function [newMat]=matCon(m,r1,r2,s)
newMat=[];
rows=size(m,1); % finds number of rows
cols=size(m,2); % finds number of columns
n=2; % will start from the second row
while n <= rows % runs the while statement to the last row of the matrix
m(n,:)=s.*(m(n-1,:))+(m(n,:)); % Does the calculations
n=n+1; % Makes the loop go to the second row
newMat=m;
end
end
Marc - your Largest function looks fine (I gad forgotten about the absolute value), but you should rename your local variable max to something else so that you don't conflict with the built-in function of the same name. In fact, you could probably simplify your code if you use the max function instead of looping over each row. Also look at how you initialize maxRow as a row of elements instead of as r which may cause a problem if no other row in the matrix has a larger element in absolute value as you will be returning a vector instead of a scalar.
Your swap function us nice and neat though you could remove the first line since you don't use r and c elsewhere.
As for matCon, I don't understand why you have a while loop. Aren't you just supposed to take the linear combination of the two rows and not all the rows, or does the algorithm require this. Why pass in r1 and r2 if you are not going to use them?
Why not simply say
function maxRow=Largest(M,r,c)
[maxValue, maxRow] = max(M(r:end, c));
You can do it in two lines instead of all that complicated code you used.
I got the original function to work, thank you so much! It was the matCon function. I completely overlooked the fact that I had it running through the entire matrix.
Glad you got it to work, Marc!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 27 Nov 2014

Commented:

on 30 Nov 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!