tttthomasssss
tttthomasssss

Reputation: 5971

Matlab, matrix containing random numbers within specified range

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.

Upvotes: 0

Views: 27706

Answers (3)

Punct
Punct

Reputation: 11

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

Upvotes: 1

sweetapus
sweetapus

Reputation: 19

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.

Upvotes: 1

Karthik V
Karthik V

Reputation: 1897

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.

Upvotes: 7

Related Questions