How to generate a random REAL NUMBER VECTOR with a range?

2 Ansichten (letzte 30 Tage)
Ariel Chou
Ariel Chou am 27 Okt. 2017
Kommentiert: Ariel Chou am 27 Okt. 2017
How to generate a random REAL NUMBER VECTOR with a range?
I can only generate a random INTEGER vector with a range, which is randi([low, high], column, row) but I don't know how to create a random number vector with a range... I tried rand([low, high], column, row) and an error message popped out saying "Error using rand Size inputs must be scalar."

Akzeptierte Antwort

michio
michio am 27 Okt. 2017
Bearbeitet: michio am 27 Okt. 2017
In general, you can generate N random numbers in the interval (a,b) with the formula
 r = a + (b-a).*rand(N,1);

Weitere Antworten (1)

Image Analyst
Image Analyst am 27 Okt. 2017
Try this. I do it both ways: to get integers and floating point numbers. Use whichever you want:
low = 10;
high = 20;
numElements = 80;
% Generate integer vector.
rIntegers = randi([low, high], 1, numElements)
% Generate floating point (fractional) number vector.
rDoubles = low + (high-low) * rand(1, numElements)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by