Rand () function usage in MATLAB

Source: Internet
Author: User

I. Theoretical preparation

Matlab function randn: a random number or matrix function that generates a normal distribution with the mean value 0, variance σ ^ 2 = 1, and standard deviation σ = 1.

Usage: Y = randn (n) returns a matrix of N * n random items. If n is not the number, an error is returned.
Y = randn (m, n) or Y = randn ([m n]) returns a random matrix of M * n.

Y = randn (M, N, P ,...) or y = randn ([m n p...]), generate a random array (it seems like a 3D array. See the following example ).

  1: >> rand(1,2,3)
  2: ans(:,:,1) =
  3:    0.445586200710899   0.646313010111265
  4: ans(:,:,2) =
  5:    0.709364830858073   0.754686681982361
  6: ans(:,:,3) =
  7:    0.276025076998578   0.679702676853675

Y = randn (SIZE (A), returns a random array with the same dimension as.

Randn

S = randn ('state'). It is estimated that random seeds are initialized in C ++.

Ii. Example Analysis

Generate a matrix of the specified Mean and variance for a random distribution: multiply the result generated by randn by the standard deviation, and then add the expected mean. For example, a random number of 5*5 with an average value of 0.6 and a variance of 0.1 is generated as follows:

  1: x = .6 + sqrt(0.1) * randn(5)

Other similar functions: Rand, randperm, sprand, sprandn

Iii. Expansion

Use MATLAB to randomly generate 60 positive numbers 1 + fix (365 * rand (365) between 1 and. Fix is the entire function.
Use the rand function to randomly retrieve 100 numbers from-1 to 2 x1, x2,..., x = rand (1,100) * 2-1.

Rand () function usage in MATLAB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.