function parm = setgmn ( meanv, covm, p )

%*****************************************************************************80
%
%% setgmn() sets data for the generation of multivariate normal deviates.
%
%  Discussion:
%
%    This procedure places P, MEANV, and the Cholesky factorization of
%    COVM in GENMN.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    31 March 2013
%
%  Author:
%
%    Original FORTRAN77 version by Barry Brown, James Lovato.
%    MATLAB version by John Burkardt.
%
%  Input:
%
%    real MEANV(P), the means of the multivariate
%    normal distribution.
%
%    real COVM(P,P).  The covariance
%    matrix of the multivariate distribution. 
%
%    integer P, the number of dimensions.
%
%  Output:
%
%    real PARM(P*(P+3)/2+1), parameters needed to generate
%    multivariate normal deviates.
%
  parm = zeros ( p * ( p + 3 ) / 2 + 1, 1 );

  if ( p <= 0 )
    fprintf ( 1, ' \n' );
    fprintf ( 1, 'SETGMN - Fatal error!\n' );
    fprintf ( 1, '  P was not positive.\n' );
    error ( 'SETGMN - Fatal error!\n' );
  end
%
%  Store P.
%
  parm(1) = p;
%
%  Store MEANV.
%
  for i = 2 : p + 1
    parm(i) = meanv(i-1);
  end
%
%  Compute the Cholesky decomposition.
%
  [ covm_fac, info ] = spofa ( covm, p, p );

  if ( info ~= 0)
    fprintf ( 1, ' \n' );
    fprintf ( 1, 'SETGMN - Fatal error!\n' );
    fprintf ( 1, '  SPOFA finds COVM not positive definite.\n' );
    error ( 'SETGMN - Fatal error!\n' );
  end
%
%  Store the upper half of the Cholesky factor.
%
  icount = p + 1;

  for i = 1 : p
    for j = i : p
      icount = icount + 1;
      parm(icount) = covm_fac(i,j);
    end
  end

  return
end
