Matlab, matrix containing random numbers within sp

2019-01-27 11:08发布

问题:

I am absolutely new to Matlab and am trying to create an m-by-n matrix containing numbers within a specified range (ie. between -1 and 1).

Is there an equivalent function to rand(m, n) where I can specify the range myself or would I need to explicitely create a bunch of random numbers (as ie. was described in this answer) and create a matrix from them?

Any pointers to relevant Documentation, etc. highly appreciated.

回答1:

This is straight from Matlab's documentation for rand. Is this want you want?

Example 1

Generate values from the uniform distribution on the interval [a, b]:

r = a + (b-a).*rand(100,1);

Try reading the Matlab documentation by entering doc rand in the command window. It is really informative and user friendly.



回答2:

Perhaps an easier way of doing that would be to type in

r = randi ( [a b], m , n )

where a = -1 (or lower limit), b = 1 (or upper limit), m x n as specified. You might need to use randint if randi doesn't work.



回答3:

6 *rand(4) => creates a 4x4 matrix with random numbers between 0 and 6

6 *rand(4,5) => creates a 4x5 matrix with random numbers between 0 and 6

randi (5,3) => creates a 3x3 matrix with random integers between 0 and 5

2+(6-2)*rand(3) => creates a 3x3 matrix with random numbers between 2 and 6