r/matlab•
BigLarph

If Statement for Gaussian Elimination Program

I'm trying to modify a function that does Gaussian Elimination on an n x n matrix A and returns U to work on a non square, singular matrix as well but I am having trouble writing my for loops.

Here's what I have so far:

[r c]=size(A); %get dimensions of matrix A rnk=rank(A);

for j=1:(c-1) %evaluates all but the last column
for i=(j+1):r %evaluates all rows starting at row 2

while i~=? %when you're not in the 1st or last row, %do elimination and keep moving pivot to %the right while staying in the same row

      if A(i-1,j)==0 %performs row exchange on pivot
         %since A is not square, something other than j,j
        
           A([i j],:)=A([j i],:); end
  
  if (A(i-1,j)~=0) %if pivot doesnt = 0, perform elim
            A(i,:)=A(i,:)-A(j,:).*(A(i,j)/A(j,j)); end
        
        
       end

  end

end

What I want the for loops to do is count in a way that accounts for the matrix not being square. I went through a 3x5 full rank matrix to get an idea of what positions the pivots were in so I would be able to get an idea of how the iterations should look and the pivots were in the (1,1),(2,2),(2,3),(2,4), and (3,5) positions. This tells me the (i+1,j+1) position will get eliminated up until last position (3,5) where elimination will stop.

So what I need is for the for loop to keep incrementing the j variable while keeping i constant in the columns between the first and last column. Is the while loop the right way to do this? If so, would anyone be able to give me an idea of what the while loop would look like to accomplish this?

Posted Dec 2, 2012, 7:42 PM UTC

34
Loading comments…