function covar = setcov ( p, var, corr )

%*****************************************************************************80
%
%% setcov() sets a covariance matrix from variance and common correlation.
%
%  Discussion:
%
%    This procedure sets the covariance matrix from the variance and
%    common correlation.
%
%  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:
%
%    integer P, the number of variables.
%
%    real VAR(P), the variances.
%
%    real CORR, the common correlaton.
%
%  Output:
%
%    real COVAR(P,P), the covariance matrix.
%
  covar = zeros ( p, p );

  for i = 1 : p
    for  j = 1 : p
      if ( i == j )
        covar(i,j) = var(i);
      else
        covar(i,j) = corr * sqrt ( var(i) * var(j) );
      end
    end
  end

  return
end
