title: Givens rotation QR decomposition
body: <p>I'm trying to create a function that computes the Givens Rotation QR decomposition, following this pseudo-code.</p>
  
  <p><a href="https://i.stack.imgur.com/o3sL1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/o3sL1.png" alt="enter image description here"></a></p>
  
  <pre><code>function [Q,R] = givens(A)
  [m,n] = size(A);
  indexI = zeros(m,n);
  indexJ = zeros(m,n);
  C = zeros(m,n);
  S = zeros(m,n);
  for i = 1:n
      for j = i+1:m
          c = A(i,i)/((A(i,i))^2 + (A(j,i)^2))^0.5;
          s = A(j,i)/((A(i,i))^2 + (A(j,i)^2))^0.5;
          A(i,:) = c*A(i,:) + s*A(j,:);
          A(j,:) = -s*A(i,:) + c*A(j,:);
          indexI(j,i) = i;
          indexJ(j,i) = j;
          C(j,i) = c;
          S(j,i) = s;
      end
  end
  R = A;
  Q = eye(m);
  for i = 1:n
      for j= j+1:m
          Q(:,i) = c*Q(:,i) + s*Q(:,j);
          Q(:,j) = -s*Q(:,i) + c*Q(:,j);
      end
  end
  </code></pre>
  
  <p>However, the R matrix that I get, is not upper triangular. I can't seem to find the mistake here.</p>