Home  >  Article  >  How to use randn function in matlab

How to use randn function in matlab

藏色散人
藏色散人Original
2020-02-14 15:38:2042317browse

How to use randn function in matlab

Usage of randn function in matlab

randn function in MATLAB

matlab function

randn: A function that generates normally distributed random numbers or matrices

randn: Generates a positive mean with a mean of 0, a variance σ^2 = 1, and a standard deviation σ = 1 A function of a state distribution of random numbers or matrices.

Usage:

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

Returns a quantity that changes every time.

s = randn('state')

Example

Example 1. R = randn(3,4) will generate a matrix

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 randomly distributed matrix with specified mean and variance: Just multiply the result produced by randn by the standard deviation and then add the expected mean. For example, the method to generate a 5*5 random number with a mean of 0.6 and a variance of 0.1 is as follows:

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

Other similar functions: rand, randperm, sprand, sprandn

Uniformly distributed random numbers or matrices

Syntax

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

Description

rand function generates an array composed of random numbers uniformly distributed between (0, 1)

Y = rand(n) returns an n x n random matrix. If n is not a quantity, an error message is returned.

Y = rand(m,n) or Y = rand([m n]) Return A m x n random matrix

Y = rand(m,n,p,...) or Y = rand([m n p...]) generates a random array

Y = rand (size(A)) returns a random matrix

1 with the same size as A, rand(3)*-2 rand(3) is a 3*3 random matrix (the value range is 0~1 between) Then multiply each number by -2

2, and use matlab to randomly generate 60 positive numbers between 1 and 365 1 fix(365*rand(1,60));

3, use rand function to randomly pick 100 numbers x1, x2,..., x = rand(1,100) * 2 - 1

from -1 to 1

The above is the detailed content of How to use randn function in matlab. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn