Create random values between two decimal values

7 ビュー (過去 30 日間)
Adrian
Adrian 2012 年 10 月 10 日
I have written this code so far:
%create initial elastic net points as evenly spaced in horizontal position
%with random vertical components.
%create the initial matrix
en_points = zeros(NUM_EN_POINTS,2);
%make the x comp evenly spaced horizontally spanning the length of cities
en_points(1:NUM_EN_POINTS,1) = transpose(linspace(0,1,NUM_EN_POINTS));
%make the y comp a random value between the cities
en_points(1:NUM_EN_POINTS,2) = rand();
What I am trying to do is make column 2 be a random variable between two decimal values, which are (0.5 - l, 0.5 + l).
Is this possible?

採用された回答

Razvan
Razvan 2012 年 10 月 10 日
en_points(:,2) = (0.5 - l) + 2*l*rand(NUM_EN_POINTS,1);

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2012 年 10 月 10 日
use unifrnd from the Statistics Toolbox
en_points(1:NUM_EN_POINTS,2) = unifrnd(.5 - l,.5 + l,NUM_EN_POINTS,1);
OR
en_points(1:NUM_EN_POINTS,2) = .5 - l + 2*l*rand(NUM_EN_POINTS,1);

Image Analyst
Image Analyst 2012 年 10 月 10 日
Perhaps you overlooked the first example in the help for rand(). Here it is:
Example 1
Generate values from the uniform distribution on the interval [a, b]:
r = a + (b-a).*rand(100,1);

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by