Hello! I want my 50 inputs to take a random value from a set of numbers (already defined) with an equal probability..

1 回表示 (過去 30 日間)
These 50 inputs will be undefined. Those will take a random value from [0 0.1 0.2 0.3 0.4 0.5] with an equal probability. So in the end these 50 will have values from [0 0.1 0.2 0.3 0.4 0.5] selected randomly with equal probability. This can be done with a nested for loop. Can you please explain how?

採用された回答

Guillaume
Guillaume 2018 年 3 月 23 日
I don't see why you'd want to use a loop.
value_set = [0, 0.1, 0.2, 0.3, 0.4, 0.5];
numvalues = 50;
random_selection = value_set(randi(numel(value_set), 1, numvalues)) %select numvalues out of value_set with uniform probability.
  3 件のコメント
Guillaume
Guillaume 2018 年 3 月 23 日
編集済み: Guillaume 2018 年 3 月 23 日
Yes, as documented randi selects numbers uniformly. You can easily check by changing numvalues to a large number and plotting the histogram of the selected values.
Neje
Neje 2018 年 3 月 24 日
It works. Thanks a lot! :)

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by