matlab中二元正态分布函数.docx
matlab中二元正态分布函数matlab中二元正态分布函数 function data1, data2 = twogaussian(n1,mu1,cov1,n2,mu2,cov2); % % data1, data2 = twogaussian(n1,mu1,sigma1,n2,mu2,sigma2); % % Function to simulate data from 2 Gaussian densities in d dimensions % and to plot the data in the first 2 dimensions % % INPUTS: % n1, n2: two integers, size of data set 1 and 2 respectively % mu1, mu2: two vectors of dimension 1 x d, means % for data set 1 and 2 % cov1, cov2: two matrices of dimension d x d, covariance % matrices for data set 1 and 2 respectively % % OUTPUTS: % data1: n1 x d matrix of data for data set 1 % data2: n2 x d matrix of data for data set 2 % check that the dimensionality of the mu's and sigma's are consistent d1 = length(mu1); d2 = length(mu2); if (d1=d2) error('means are of different lengths'); end; d = length(mu1); % d is the dimensionality of the data d1 d2 = size(cov1); if (d1=d2) error('cov1 is a non-square covariance matrix'); end; if (d1=d) error('cov1 is of different dimensionality to mu1'); end; d1 d2 = size(cov2); if (d1=d2) error('cov2 is a non-square covariance matrix'); end; if (d1=d) error('cov2 is of different dimensionality to mu2'); end; % Call the function mvnrnd.m to generate the two data sets data1 = mvnrnd(mu1,cov1,n1); data2 = mvnrnd(mu2,cov2,n2); % Now plot the two data sets as a two-dimensional scatter plot % if d = 2: plot dimension1 on the xaxis and dimension 2 on the % yaxis. Plot the points from data1 as green circles 'o', and the % points from data2 as red crosses 'x'. if . figure % open a figure window plot(data1(:,1),data1(:,2),'b.');. % now plot data1 axis(-6 6 -6 6); % fix the lengths of the axes hold % hold the figure to overlay a 2nd plot plot(data2(:,1),data2(:,2),'rx');% now plot data 2 xlabel('Dimension 1'); ylabel('Dimension 2'); title('Simulation of two-class Gaussian data in two dimensions'); end function r = mvnrnd(mu,sigma,cases); %MVNRND Random matrices from the multivariate normal distribution. % R = MVNRND(MU,SIGMA,CASES) returns a matrix of random numbers % chosen from the multivariate normal distribution with mean vector, % MU, and covariance matrix, SIGMA. CASES is the number of rows in R. % % SIGMA is a square positive definite matrix with size equal to % the length of MU % Adapted from Mathworks function m1 n1 = size(mu); c = max(m1 n1); if m1 .* n1 = c error('Mu must be a vector.'); end m n = size(sigma); if m = n error('Sigma must be square'); end if m = c error('The length of mu must equal the number of rows in sigma.'); end T p = chol(sigma); if p = 0 error('Sigma must be a positive definite matrix.'); end if m1 = c mu = mu' end mu = mu(ones(cases,1),:); r = randn(cases,c) * T + mu;