How to create a vector that assembles 30000 sets of parameters at random from the specified ranges

1 回表示 (過去 30 日間)
Hello,
I'm trying create a vector that assembles 30000 sets of parameters at random from the specified ranges.
I have a total of 30 parameters and each parameter values will be randomly selected from the specified ranges.
My initial code is something alone the line below;
par1 = 0.01+(0.2-0.01).*rand(30000,1); %when min=0.01 and max=0.2
par2 = 0.1+(0.5-0.1).*rand(30000,1); %when min=0.1 and max=0.5
% repeat this until par30
par = [par1 par2 par30]
Instead of doing this, is there a more efficient way to write the code? maybe using loop?
Thank you in advance,
  3 件のコメント
Jung
Jung 2019 年 10 月 22 日
30 parameter boundaries are defined based on literatures. There is no specific patterns or trends.
Parameter 1 simply has a minimum boundary of 0.01 and a maximum boundary of 0.2 whereas parameter 2 has a min boundary of 0.1 and a maximum boundary of 0.5.
Adam Danz
Adam Danz 2019 年 10 月 22 日
Ah, I see. I thought you wanted to automate the bounds as well. That would be fairly easy to do if there were a pattern.
I suggest making a simple local function where you just need to enter the bounds and the number of random samples you want. Then you just need to call that function for each param. See answer.

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2019 年 10 月 22 日
編集済み: Adam Danz 2019 年 10 月 23 日
You could writing a function that receives the lower and upper bounds as inputs along with the number of values to choose.
function d = boundedRandomNumbers(LB,UB,N)
% Draws N random samples between lower bound LB and upper bound UB
d = rand(N,1)*(UB-LB) + LB;
To get 30k values between the bounds,
par1 = boundedRandomNumbers(0.01, 0.02, 30000);
par2 = boundedRandomNumbers(0.1, 0.5, 30000);
If your bounds were stored in an 30x2 matrix, you could even loop through each row of the matrix and store the parameter values in a 2nd matrix.
% bounds is 30 x 2 matrix of [lower, upper] bounds
parAll = zeros(30,30000);
for i =1:30
parAll(i,:) = boundedRandomNumbers(bounds(i,1), bounds(i,2), 30000);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by