function [Q,R] = QRJG(A)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   QRJG
%
%   This algorithm computes the QR factorization of a matrix using the
%   standard Gram-Schmidt process.
%
%   Inputs:
%   1. A (nxn) matrix.
%
%   Ouputs:
%   1. Q unitary matrix with columns of vectors lying in the span of
%   columns of A.
%   2. R upper triangular matrix with components of corresponding to
%   expansion of columns of A in basis of columns of Q.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Extracting information from A and allocating space for matries
[~,n]=size(A);
Q=zeros(n,n);
R=zeros(n,n);


%% Main loop
for j=1:n,
    v=A(:,j);
    
    for i=1:j-1,
        qi=Q(:,i);
        aj=A(:,j);
        R(i,j)=qi'*aj;
        v=v-R(i,j)*qi;
    end
    
    R(j,j)=norm(v);
    qj=v/R(j,j);
    Q(:,j)=qj;
end


end

