function U = mgs3 ( A )

%*****************************************************************************80
%
%% mgs3() applies the modified Gram-Schmidt algorithm with normalization.
%
%  Discussion:
%
%    The output of this function will be a set of orthonormal vectors,
%    such that U(:,i) dot U(:,j) = delta(i,j).
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    07 December 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real A(M,N), a matrix of N vectors.
%
%  Output:
%
%    real U(M,K), a matrix with orthonormal columns.
%
  [ m, n ] = size ( A );

  k = 0;
  U = zeros(m,k);

  for j = 1 : n

    v = A(:,j);
    vnorm = norm ( v );

    if ( 0 < vnorm )

      k = k + 1;
      v = v / norm ( v );
      U(:,k) = v;

      for j2 = j + 1 : n
        v2 = A(:,j2);
        p = v' * v2;
        v2 = v2 - p * v;
        A(:,j2) = v2;
      end

    end

  end

  return
end
