Matlab, matrix containing random numbers within specified range

你离开我真会死。 提交于 2019-12-18 07:22:38

问题


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



来源:https://stackoverflow.com/questions/14482951/matlab-matrix-containing-random-numbers-within-specified-range

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