完整版Matlab各种随机数设置.docx

上传人:b****5 文档编号:4186047 上传时间:2022-11-28 格式:DOCX 页数:11 大小:105.43KB
下载 相关 举报
完整版Matlab各种随机数设置.docx_第1页
第1页 / 共11页
完整版Matlab各种随机数设置.docx_第2页
第2页 / 共11页
完整版Matlab各种随机数设置.docx_第3页
第3页 / 共11页
完整版Matlab各种随机数设置.docx_第4页
第4页 / 共11页
完整版Matlab各种随机数设置.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

完整版Matlab各种随机数设置.docx

《完整版Matlab各种随机数设置.docx》由会员分享,可在线阅读,更多相关《完整版Matlab各种随机数设置.docx(11页珍藏版)》请在冰豆网上搜索。

完整版Matlab各种随机数设置.docx

完整版Matlab各种随机数设置

Matlab各种随机数设置

randn(伪随机正态分布数)

Normallydistributedpseudorandomnumbers

Syntax

r=randn(n)

randn(m,n)

randn([m,n])

randn(m,n,p,...)

randn([m,n,p,...])

randn(size(A))

r=randn(...,'double')

r=randn(...,'single')

Description

r=randn(n)returnsann-by-nmatrixcontainingpseudorandomvaluesdrawnfromthestandardnormaldistribution.randn(m,n)orrandn([m,n])returnsanm-by-nmatrix.randn(m,n,p,...)orrandn([m,n,p,...])returnsanm-by-n-by-p-by-...array.randnreturnsascalar.randn(size(A))returnsanarraythesamesizeasA.

r=randn(...,'double')orr=randn(...,'single')returnsanarrayofnormalvaluesofthespecifiedclass.

NoteThesizeinputsm,n,p,...shouldbenonnegativeintegers.Negativeintegersaretreatedas0.

Thesequenceofnumbersproducedbyrandnisdeterminedbytheinternalstateoftheuniformpseudorandomnumbergeneratorthatunderliesrand,randi,andrandn.randnusesoneormoreuniformvaluesfromthatdefaultstreamtogenerateeachnormalvalue.Controlthedefaultstreamusingitspropertiesandmethods.

NoteInversionsofMATLABpriorto7.7(R2008b),youcontrolledtheinternalstateoftherandomnumberstreamusedbyrandnbycallingrandndirectlywiththe'seed'or'state'keywords.

Examples

Generatevaluesfromanormaldistributionwithmean1andstandarddeviation2.

r=1+2.*randn(100,1);

Generatevaluesfromabivariatenormaldistributionwithspecifiedmeanvectorandcovariancematrix.

mu=[12];

Sigma=[1.5;.52];R=chol(Sigma);

z=repmat(mu,100,1)+randn(100,2)*R;

ReplacethedefaultstreamatMATLABstartup,usingastreamwhoseseedisbasedonclock,sothatrandnwillreturndifferentvaluesindifferentMATLABsessions.ItisusuallynotdesirabletodothismorethanonceperMATLABsession.

RandStream.setDefaultStream...

(RandStream('mt19937ar','seed',sum(100*clock)));

randn(1,5)

Savethecurrentstateofthedefaultstream,generate5values,restorethestate,andrepeatthesequence.

defaultStream=RandStream.getDefaultStream;

savedState=defaultStream.State;

z1=randn(1,5)

defaultStream.State=savedState;

z2=randn(1,5)%containsexactlythesamevaluesasz1

Normrnd(随机正态分布数)

Normalrandomnumbers

Syntax

R=normrnd(mu,sigma)

R=normrnd(mu,sigma,m,n,...)

R=normrnd(mu,sigma,[m,n,...])

Description

R=normrnd(mu,sigma)generatesrandomnumbersfromthenormaldistributionwithmeanparametermuandstandarddeviationparametersigma.muandsigmacanbevectors,matrices,ormultidimensionalarraysthathavethesamesize,whichisalsothesizeofR.Ascalarinputformuorsigmaisexpandedtoaconstantarraywiththesamedimensionsastheotherinput.

R=normrnd(mu,sigma,m,n,...)orR=normrnd(mu,sigma,[m,n,...])generatesanm-by-n-by-...array.Themu,sigmaparameterscaneachbescalarsorarraysofthesamesizeasR.

Examples

n1=normrnd(1:

6,1./(1:

6))

n1=

2.16502.31343.02504.08794.86076.2827

n2=normrnd(0,1,[15])

n2=

0.05911.79710.26410.8717-1.4462

n3=normrnd([123;456],0.1,2,3)

n3=

0.92991.93612.9640

4.12465.05775.9864

randperm(RandStream)(区域内的所有整数的随机分布)

Randompermutation

randperm(s,n)

Description

randperm(s,n)generatesarandompermutationoftheintegersfrom1ton.Forexample,randperm(s,6)mightbe[245613].randperm(s,n)usesrandomvaluesdrawnfromtherandomnumberstreams.

betarnd(贝塔分布)

贝塔分布是一个作为伯努利分布和二项式分布的共轭先验分布的密度函数

Syntax

R=betarnd(A,B)

R=betarnd(A,B,m,n,...)

R=betarnd(A,B,[m,n,...])

Description

R=betarnd(A,B)generatesrandomnumbersfromthebetadistributionwithparametersspecifiedbyAandB.AandBcanbevectors,matrices,ormultidimensionalarraysthathavethesamesize,whichisalsothesizeofR.AscalarinputforAorBisexpandedtoaconstantarraywiththesamedimensionsastheotherinput.

R=betarnd(A,B,m,n,...)orR=betarnd(A,B,[m,n,...])generatesanm-by-n-by-...arraycontainingrandomnumbersfromthebetadistributionwithparametersAandB.AandBcaneachbescalarsorarraysofthesamesizeasR.

Examples

a=[11;22];

b=[12;12];

r=betarnd(a,b)

r=

0.69870.6139

0.91020.8067

r=betarnd(10,10,[15])

r=

0.59740.47770.55380.54650.6327

r=betarnd(4,2,2,3)

r=

0.39430.61010.5768

0.59900.27600.5474

Binornd(二项式分布)

二项分布(binomialdistribution)就是对这类只具有两种互斥结果的离散型随机事件的规律性进行描述的一种概率分布。

Syntax

R=binornd(N,P)

R=binornd(N,P,m,n,...)

R=binornd(N,P,[m,n,...])

Description

R=binornd(N,P)generatesrandomnumbersfromthebinomialdistributionwithparametersspecifiedbythenumberoftrials,N,andprobabilityofsuccessforeachtrial,P.NandPcanbevectors,matrices,ormultidimensionalarraysthathavethesamesize,whichisalsothesizeofR.AscalarinputforNorPisexpandedtoaconstantarraywiththesamedimensionsastheotherinput.

R=binornd(N,P,m,n,...)orR=binornd(N,P,[m,n,...])generatesanm-by-n-by-...arraycontainingrandomnumbersfromthebinomialdistributionwithparametersNandP.NandPcaneachbescalarsorarraysofthesamesizeasR.

Algorithm

ThebinorndfunctionusesthedirectmethodusingthedefinitionofthebinomialdistributionasasumofBernoullirandomvariables.

Examples

n=10:

10:

60;

r1=binornd(n,1./n)

r1=

210112

r2=binornd(n,1./n,[16])

r2=

012131

r3=binornd(n,1./n,1,6)

r3=

011103

chi2rnd(卡方分布)

若n个相互独立的随机变量ξ₁、ξ₂、……、ξn,均服从标准正态分布,则这n个服从标准正态分布的随机变量的平方和构成一新的随机变量,其分布规律称为χ²分布

Syntax

R=chi2rnd(V)

R=chi2rnd(V,m,n,...)

R=chi2rnd(V,[m,n,...])

Description

R=chi2rnd(V)generatesrandomnumbersfromthechi-squaredistributionwithdegreesoffreedomparametersspecifiedbyV.Vcanbeavector,amatrix,oramultidimensionalarray.RisthesamesizeasV.

R=chi2rnd(V,m,n,...)orR=chi2rnd(V,[m,n,...])generatesanm-by-n-by-...arraycontainingrandomnumbersfromthechi-squaredistributionwithdegreesoffreedomparameterV.VcanbeascalaroranarrayofthesamesizeasR.

Examples

Notethatthefirstandthirdcommandsarethesame,butaredifferentfromthesecondcommand.

r=chi2rnd(1:

6)

r=

0.00373.03777.81420.90213.20199.0729

r=chi2rnd(6,[16])

r=

6.52492.622612.24973.03886.31335.0388

r=chi2rnd(1:

6,1,6)

r=

0.76386.09550.82733.25061.546910.9197

Copularnd(连接函数分布)

Copula函数描述的是变量间的相关性,实际上是一类将联合分布函数与它们各自的边缘分布函数连接在一起的函数

Syntax

U=copularnd('Gaussian',rho,N)

U=copularnd('t',rho,NU,N)

U=copularnd('family',alpha,N)

Description

U=copularnd('Gaussian',rho,N)returnsNrandomvectorsgeneratedfromaGaussiancopulawithlinearcorrelationparametersrho.Ifrhoisap-by-pcorrelationmatrix,Uisann-by-pmatrix.Ifrhoisascalarcorrelationcoefficient,copularndgeneratesUfromabivariateGaussiancopula.EachcolumnofUisasamplefromaUniform(0,1)marginaldistribution.

U=copularnd('t',rho,NU,N)returnsNrandomvectorsgeneratedfromatcopulawithlinearcorrelationparametersrhoanddegreesoffreedomNU.Ifrhoisap-by-pcorrelationmatrix,Uisann-by-pmatrix.Ifrhoisascalarcorrelationcoefficient,copularndgeneratesUfromabivariatetcopula.EachcolumnofUisasamplefromaUniform(0,1)marginaldistribution.

U=copularnd('family',alpha,N)returnsNrandomvectorsgeneratedfromthebivariateArchimedeancopuladeterminedbyfamily,withscalarparameteralpha.familyisClayton,Frank,orGumbel.Uisann-by-2matrix.EachcolumnofUisasamplefromaUniform(0,1)marginaldistribution.

Examples

DeterminethelinearcorrelationparametercorrespondingtoabivariateGaussiancopulahavingarankcorrelationof-0.5.

tau=-0.5

rho=copulaparam('gaussian',tau)

rho=

-0.7071

%Generatedependentbetarandomvaluesusingthatcopula

u=copularnd('gaussian',rho,100);

b=betainv(u,2,2);

%Verifythatthesamplehasarankcorrelation

%approximatelyequaltotau

tau_sample=corr(b,'type','kendall')

tau_sample=

1.0000-0.4537

-0.45371.0000

Gamrnd(伽马分布)

伽玛分布(Gammadistribution)是统计学的一种连续概率函数。

Gamma分布中的参数α,称为形状参数(shapeparameter),β称为尺度参数(scaleparameter)。

Syntax

R=gamrnd(A,B)

R=gamrnd(A,B,m,n,...)

R=gamrnd(A,B,[m,n,...])

Description

R=gamrnd(A,B)generatesrandomnumbersfromthegammadistributionwithshapeparametersinAandscaleparametersinB.AandBcanbevectors,matrices,ormultidimensionalarraysthatallhavethesamesize.AscalarinputforAorBisexpandedtoaconstantarraywiththesamedimensionsastheotherinput.

R=gamrnd(A,B,m,n,...)orR=gamrnd(A,B,[m,n,...])generatesanm-by-n-by-...arraycontainingrandomnumbersfromthegammadistributionwithparametersAandB.AandBcaneachbescalarsorarraysofthesamesizeasR.

Examples

n1=gamrnd(1:

5,6:

10)

n1=

9.113212.843124.802538.5960106.4164

n2=gamrnd(5,10,[15])

n2=

30.948633.566733.683755.201446.8265

n3=gamrnd(2:

6,3,1,5)

n3=

12.871511.30683.098215.601221.6739

Frnd(F分布,两个卡方分布的自由度)

F分布定义为:

设X、Y为两个独立的随机变量,X服从自由度为k1的卡方分布,Y服从自由度为k2的卡方分布,这2个独立的卡方分布被各自的自由度除以后的比率这一统计量的分布。

即:

上式F服从第一自由度为k1,第二自由度为k2的F分布

Syntax

R=frnd(V1,V2)

R=frnd(V1,V2,m,n,...)

R=frnd(V1,V2,[m,n,...])

Description

R=frnd(V1,V2)generatesrandomnumbersfromtheFdistributionwithnumeratordegreesoffreedomV1anddenominatordegreesoffreedomV2.V1andV2canbevectors,matrices,ormultidimensionalarraysthatallhavethesamesize.AscalarinputforV1orV2isexpandedtoaconstantarraywiththesamedimensionsastheotherinput.

R=frnd(V1,V2,m,n,...)orR=frnd(V1,V2,[m,n,...])generatesanm-by-n-by-...arraycontainingrandomnumbersfromtheFdistributionwithparametersV1andV2.V1andV2caneachbescalarsorarraysofthesamesizeasR.

Examples

n1=frnd(1:

6,1:

6)

n1=

0.00220.31213.0

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1