function pgm_approx_display ( filelist, n, png_filename )

%*****************************************************************************80
%
%% pgm_approx_display() approximates PGM images using N SVD vectors.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    25 December 2018
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string FILELIST, the name of a file, which lists the PGM files
%    to be packed.
%
%    integer N, the number of SVD vectors to extract.
%
%    string PNG_FILENAME, the name of the file in which the final
%    image is to be saved.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'PGM_APPROX_DISPLAY:\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Packing files from the list "%s"\n', filelist );
  fprintf ( 1, '  Extracting average and %d SVD vectors\n', n );
%
%  1: Create average and SVD vectors.
%
  [ pgms, w, h, pgm_num ] = pgms_pack ( filelist );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Packed array contains %d PGM files\n', pgm_num );
  fprintf ( 1, '  Each PGM has width %d and height %d\n', w, h );
%
%  Compute N principal component vectors.
%
  [ pc_vec, pc_val, av_vec ] = svd_vectors ( pgms, n );
%
%  2: Display first 5 faces and approximations.
%
  figure ( 1 );
  clf ( 1 );

  for k = 1 : 10

    if ( k <= 5 )
      pos = k;
    else
      pos = k + 5;
    end

    pgm = pgms(:,k);
    pgm = reshape ( pgm, w, h );
    pgm = pgm';
    subplot ( 4, 5, pos );
    imagesc ( pgm );
    colormap ( gray(256) );
    daspect ( [1,1,1] );
    set ( gca, 'Xtick', [], 'Ytick', [] );
%
%  Create a version of the face.
%
    pgm_vec = reshape ( pgm', w * h, 1 );

    proj_vec = av_vec;
    for j = 1 : n
      proj_vec = proj_vec + pc_vec(:,j) * pc_vec(:,j)' * ( pgm_vec - av_vec );
    end

    subplot ( 4, 5, pos + 5 );
    I = reshape ( proj_vec, w, h );
    I = I';
    imagesc ( I );
    colormap ( gray(256) );
    daspect ( [1,1,1] );
    set ( gca, 'Xtick', [], 'Ytick', [] );
    
  end

  label = sprintf ( 'Faces and Approximations with %d vectors', n ); 
  sgtitle ( label, 'Fontsize', 16 );
  print ( '-dpng', png_filename );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'PGM_APPROX_DISPLAY:\n' );
  fprintf ( 1, '  Normal end of execution\n' );
  return
end
