function [U,R] = myQR(V)
% Classical Gram-Schmidt
% Input  : V - nxq (n >= q)
% Output : U - nxq, has orthonormal columns s.t. Col(U) = Col(V)

[~,q] = size(V);

for j = 1:q
    
    U(:,j) = V(:,j);
    
    for k = 1:j-1
        R(k,j) = U(:,k)'*V(:,j);
        U(:,j) = U(:,j) -  R(k,j)*U(:,k);
    end
    
    R(j,j) = norm(U(:,j));
    U(:,j) = U(:,j) / R(j,j);

end

return;

