Generate random numbers matrix with different probabilities

How do I generate a 15x20 matrix with random numbers between -5 and 23 but the negative numbers (Including 0) are twice as likely to appear than positive numbers?

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 10 月 4 日
編集済み: Ameer Hamza 2020 年 10 月 4 日

0 投票

This is one way
candidates = [-5:0 -5:0 1:23]; % twice as many candidates from -5 to 0
idx = randi(numel(candidates), 15, 20);
x = candidates(idx);
To see if it really work, create a large matrix x and see the histogram of its values
candidates = [-5:0 -5:0 1:23]; % twice as many candidates from -5 to 0
idx = randi(numel(candidates), 100, 200);
x = candidates(idx);
histogram(x(:))

2 件のコメント

Sander Nys
Sander Nys 2020 年 10 月 4 日
Thank you very much!
Ameer Hamza
Ameer Hamza 2020 年 10 月 5 日
I am glad to be of help!

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

質問済み:

2020 年 10 月 4 日

コメント済み:

2020 年 10 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by