分享

最新分享

 等缘708 2012-05-09
MATLAB 中的randn函数
matlab函数randn:产生正态分布的随机数或矩阵的函数
randn
产生均值为0,方差 σ^2 = 1,标准差σ = 1的正态分布的随机数或矩阵的函数

用法:
Y = randn(n)
返回一个n*n的随机项的矩阵。如果n不是个数量,将返回错误信息。
Y = randn(m,n) 或 Y = randn([m n])
返回一个m*n的随机项矩阵。
Y = randn(m,n,p,...) 或 Y = randn([m n p...])
产生随机数组。
Y = randn(size(A))
返回一个和A有同样维数大小的随机数组。
randn
返回一个每次都变化的数量。

s = randn('state')

举例

Example 1. R = randn(3,4) 将生成矩阵

R =
1.1650 0.3516 0.0591 0.8717
0.6268 -0.6965 1.7971 -1.4462
0.0751 1.6961 0.2641 -0.7012
For a histogram of the randn distribution, see hist.

Example 2. 产生一个随机分布的指定均值和方差的矩阵:将randn产生的结果乘以标准差,然后加上期望均值即可。例如,产生均值为0.6,方差为0.1的一个5*5的随机数方式如下:

x = .6 + sqrt(0.1) * randn(5)
x =
0.8713 0.4735 0.8114 0.0927 0.7672
0.9966 0.8182 0.9766 0.6814 0.6694
0.0960 0.8579 0.2197 0.2659 0.3085
0.1443 0.8251 0.5937 1.0475 -0.0864
0.7806 1.0080 0.5504 0.3454 0.5813
其他类似函数:rand, randperm, sprand, sprandn

-------------------------------------------------------------
randn
Normally distributed random numbers and arrays

Syntax
Y = randn(n)
Y = randn(m,n)
Y = randn([m n])
Y = randn(m,n,p,...)
Y = randn([m n p...])
Y = randn(size(A))
randn
s = randn('state')

Description
The randn function generates arrays of random numbers whose elements are normally distributed with mean 0, variance σ^2 = 1 , and standard deviation σ = 1.

Y = randn(n) returns an n-by-n matrix of random entries. An error message appears if n is not a scalar. Y = randn(m,n) or Y = randn([m n]) returns an m-by-n matrix of random entries. Y = randn(m,n,p,...) or Y = randn([m n p...]) generates random arrays. Y = randn(size(A)) returns an array of random entries that is the same size as A. randn, by itself, returns a scalar whose value changes each time it's referenced. s = randn('state') returns a 2-element vector containing the current state of the normal generator. To change the state of the generator: randn('state',s)Resets the state to s. randn('state',0)Resets the generator to its initial state. randn('state',j)For integer j, resets the generator to its jth state. randn('state',sum(100*clock))Resets it to a different state each time.ExamplesExample 1. R = randn(3,4) may produce   R =
1.1650 0.3516 0.0591 0.8717
0.6268 -0.6965 1.7971 -1.4462
0.0751 1.6961 0.2641 -0.7012
For a histogram of the randn distribution, see hist. Example 2. Generate a random distribution with a specific mean and variance . To do this, multiply the output of randn by the standard deviation , and then add the desired mean. For example, to generate a 5-by-5 array of random numbers with a mean of .6 that are distributed with a variance of 0.1 x = .6 + sqrt(0.1) * randn(5)
x =
0.8713 0.4735 0.8114 0.0927 0.7672
0.9966 0.8182 0.9766 0.6814 0.6694
0.0960 0.8579 0.2197 0.2659 0.3085
0.1443 0.8251 0.5937 1.0475 -0.0864
0.7806 1.0080 0.5504 0.3454 0.5813
See Also rand, randperm, sprand, sprandn

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多