资源描述:
《matlab中二元正态分布函数.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、matlab中二元正态分布函数function[data1,data2]=twogaussian(n1,mu1,cov1,n2,mu2,cov2);%%[data1,data2]=twogaussian(n1,mu1,sigma1,n2,mu2,sigma2);%%Functiontosimulatedatafrom2Gaussiandensitiesinddimensions%andtoplotthedatainthefirst2dimensions%%INPUTS:%n1,n2:twointe
2、gers,sizeofdataset1and2respectively%mu1,mu2:twovectorsofdimension1xd,means%fordataset1and2%cov1,cov2:twomatricesofdimensiondxd,covariance%matricesfordataset1and2respectively%%OUTPUTS:%data1:n1xdmatrixofdatafordataset1%data2:n2xdmatrixofdatafordataset2
3、%checkthatthedimensionalityofthemu'sandsigma'sareconsistentd1=length(mu1);d2=length(mu2);if(d1~=d2)error('meansareofdifferentlengths');end;d=length(mu1);%disthedimensionalityofthedata[d1d2]=size(cov1);if(d1~=d2)error('cov1isanon-squarecovariancematrix
4、');end;if(d1~=d)error('cov1isofdifferentdimensionalitytomu1');end;[d1d2]=size(cov2);if(d1~=d2)error('cov2isanon-squarecovariancematrix');end;if(d1~=d)error('cov2isofdifferentdimensionalitytomu2');end;%Callthefunctionmvnrnd.mtogeneratethetwodatasetsdat
5、a1=mvnrnd(mu1,cov1,n1);data2=mvnrnd(mu2,cov2,n2);%Nowplotthetwodatasetsasatwo-dimensionalscatterplot%ifd=2:plotdimension1onthexaxisanddimension2onthe%yaxis.Plotthepointsfromdata1asgreencircles'o',andthe%pointsfromdata2asredcrosses'x'.if....figure%open
6、afigurewindowplot(data1(:,1),data1(:,2),'b.');....%nowplotdata1axis([-66-66]);%fixthelengthsoftheaxeshold%holdthefiguretooverlaya2ndplotplot(data2(:,1),data2(:,2),'rx');%nowplotdata2xlabel('Dimension1');ylabel('Dimension2');title('Simulationoftwo-clas
7、sGaussiandataintwodimensions');endfunctionr=mvnrnd(mu,sigma,cases);%MVNRNDRandommatricesfromthemultivariatenormaldistribution.%R=MVNRND(MU,SIGMA,CASES)returnsamatrixofrandomnumbers%chosenfromthemultivariatenormaldistributionwithmeanvector,%MU,andcovar
8、iancematrix,SIGMA.CASESisthenumberofrowsinR.%%SIGMAisasquarepositivedefinitematrixwithsizeequalto%thelengthofMU%AdaptedfromMathworksfunction[m1n1]=size(mu);c=max([m1n1]);ifm1.*n1~=cerror('Mumustbeavector.');end[mn]=size(sigma);ifm~=nerror('Sig