title: Converting a loop from Matlab to C
body: <p>I have a (3x5) matrix and I want to get its reduced row echelon form.
  I want to implement it in C, so I first impelemnted it in Matlab like follows:</p>
  
  <pre><code>[L,U]=lu(a);
  [m,n]=size(U);
  disp('convert elements in major diagonal to 1')
    for s=1:m
    U(s,:)=U(s,:)/U(s,s);
    end
    for j=m:-1:2
     for i=j-1:-1:1
     U(i,:)=U(i,:)-U(j,:)*(U(i,j)/U(j,j));
   end
   end
  </code></pre>
  
  <p>The above code and rref function gave the same result.
  When converting this code to C, I successfully implemented the LU decomposition and the conversion of elements in major diagonal to 1 but when imlemented these nested loops</p>
  
  <pre><code>for j=m:-1:2
     for i=j-1:-1:1
     U(i,:)=U(i,:)-U(j,:)*(U(i,j)/U(j,j));
   end
   end
  </code></pre>
  
  <p>as follows:</p>
  
  <pre><code>for(j=m-1;j&gt;0;j--){
   for(i=j-1;i=0;i--){
       for(k=0;k&lt;n;k++){
         U[i*n+k]=U[i*n+k]-(U[j*n+k]*(U[i*n+j]/U[j*n+j]));
          }
         }
        }
  </code></pre>
  
  <p>I got a wrong result. How to correct it please?</p>