how to Write a script that asks for a number n and nxn matrix with a Normal distribution with mean 0 and std 1?

1 次查看(过去 30 天)
can i do it with using randn?

采纳的回答

John D'Errico
John D'Errico 2022-11-12
Read the help for randn. Does it tell you how to generate a matrix of samples from that distribution?
help randn
RANDN Normally distributed pseudorandom numbers. R = RANDN(N) returns an N-by-N matrix containing pseudorandom values drawn from the standard normal distribution. RANDN(M,N) or RANDN([M,N]) returns an M-by-N matrix. RANDN(M,N,P,...) or RANDN([M,N,P,...]) returns an M-by-N-by-P-by-... array. RANDN returns a scalar. RANDN(SIZE(A)) returns an array the same size as A. Note: The size inputs M, N, P, ... should be nonnegative integers. Negative integers are treated as 0. R = RANDN(..., CLASSNAME) returns an array of normal values of the specified class. CLASSNAME can be 'double' or 'single'. R = RANDN(..., 'like', Y) returns an array of normal values with the same data type and complexity (real or complex) as the numeric variable Y. The sequence of numbers produced by RANDN is determined by the settings of the uniform random number generator that underlies RAND, RANDN, and RANDI. RANDN uses one or more uniform random values to create each normal random value. Control that shared random number generator using RNG. Examples: Example 1: Generate values from a normal distribution with mean 1 and standard deviation 2. r = 1 + 2.*randn(100,1); Example 2: Generate values from a bivariate normal distribution with specified mean vector and covariance matrix. mu = [1 2]; Sigma = [1 .5; .5 2]; R = chol(Sigma); z = repmat(mu,100,1) + randn(100,2)*R; Example 3: Reset the random number generator used by RAND, RANDI, and RANDN to its default startup settings, so that RANDN produces the same random numbers as if you restarted MATLAB. rng('default'); randn(1,5) Example 4: Save the settings for the random number generator used by RAND, RANDI, and RANDN, generate 5 values from RANDN, restore the settings, and repeat those values. s = rng z1 = randn(1,5) rng(s); z2 = randn(1,5) % z2 contains exactly the same values as z1 Example 5: Reinitialize the random number generator used by RAND, RANDI, and RANDN with a seed based on the current time. RANDN will return different values each time you do this. NOTE: It is usually not necessary to do this more than once per MATLAB session. rng('shuffle'); randn(1,5) See Replace Discouraged Syntaxes of rand and randn to use RNG to replace RANDN with the 'seed' or 'state' inputs. See also RAND, RANDI, RNG, RANDSTREAM, RANDSTREAM/RANDN Documentation for randn doc randn Other uses of randn codistributed/randn distributed/randn matlab/randn codistributor1d/randn gpuArray/randn RandStream/randn codistributor2dbc/randn

更多回答(1 个)

Chandler Hall
Chandler Hall 2022-11-12
The normal distribution with mean 0 and std 1, known as the standard normal distribution, is indeed the default distribution used by randn. See the random() function for examples of drawing from other distributions/parameters.

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by