Matlab, matrix containing random numbers within specified range

馋奶兔 提交于 2019-11-29 12:59:54

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.

sweetapus

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!