function [Q,R] = solution_hw5_p3b(A)
%solution_hw5_p3a Computes the QR decomposition using Modified Gram-Schmidt
%   A - The matrix to compute the QR of 
%   Q - Orthonormal matrix
%   R - Upper triangular matrix
%       Note: This uses the algorithm outlined in class and Trefethen. 
%         This also only works if m>n.

    [m, n] = size(A);
    
    R = zeros(n, n);
    Q = zeros(m, n);
    
    V = A(:, 1:n);
    
    for i=1:n
        R(i, i) = norm(V(:, i), 2);
        Q(:, i) = V(:, i)./R(i,i);
        
        for j=i+1:n
            R(i, j) = dot(Q(:,i), V(:, j));
            V(:, j) = V(:, j) - R(i, j)*Q(:, i);
        end
    end
end

