matlab中rand('state',123) s=sign(rand(1,1000)-.5) n=sum(s==-1

问题描述:

matlab中rand('state',123) s=sign(rand(1,1000)-.5) n=sum(s==-1)具体是什么意思
1个回答 分类:综合 2014-11-30

问题解答:

我来补答
and('state',123)
\x05=>用'state'参数可以控制随机数流的内部状态.2010bx64下帮助中说2008b后仍然支持但不推荐,但实际无法执行.提示如下:
>> z=rand('state',123)
Error using ==> rand
Too many output arguments.
后面参数123这样给是要生成一个123x123的矩阵了.
In versions of MATLAB prior to 7.7 (R2008b),you controlled the internal state of the random number stream used by rand by calling rand directly with the 'seed','state',or 'twister' keywords.That syntax is still supported for backwards compatibility,but is not recommended.To update your code,see Updating Your Random Number Generator Syntax in the MATLAB Mathematics documentation.
n=sum(s==-1)
\x05=>s==-1是逻辑表达式,会按每元素的逻辑表达式成立与否得出对应位置的1/0,所以上面求出的s变量中-1的位置全部替换成1,而其它值都是0.
\x05sum是求和,对矢量直接就是加和,这里就是求s中-1值的个数.运行了一下得到500,很完美的验证了均匀随机数生成算法rand相对于0.5的均衡性(因为默认rand是生成0~1随机数,应该在中值0.5左右有等数量的样本).
再问: 亲,对于 z=rand('state',123)中的123,运行后,最后的n=490,改为 z=rand('state',1)后,运行n=500,这是为什么啊
 
 
展开全文阅读
剩余:2000
上一页:hfftbjhg