Probability in random numbers!

How to create a random 2D array that each cell has different probabilities to be either 0 or 1 for example? In other words, how to t create a 2D array with each cell having a probability of 0.1 to be 1's and 0.9 probability to be 0's.

回答 (2 件)

Walter Roberson
Walter Roberson 2019 年 8 月 5 日

1 投票

rand(m, n) <= 0.1
would be true (1) with probability 0.1
Adam Danz
Adam Danz 2019 年 8 月 5 日
編集済み: Adam Danz 2019 年 8 月 7 日

0 投票

randsrc(5,4,[[0,1];[.9,.1]])
% 5 x 4 array
% .9 prob. of 0
% .1 prob of 1
*Requires communications toolbox

3 件のコメント

Adam Danz
Adam Danz 2019 年 8 月 5 日
編集済み: Adam Danz 2019 年 8 月 5 日
Here's a little test to see if the probabilities are roughly correct. Run this line lots of times and the values should all hover around 10% since there's a 10% probability of 1 and 90% propbability of 0.
mean(randsrc(10000000,1,[[0,1];[.9,.1]]))
Mina Mansour
Mina Mansour 2019 年 8 月 6 日
It keeps on telling me I need a communications toolbox.
Adam Danz
Adam Danz 2019 年 8 月 6 日
Use Walter Roberson's answer if you don't have communications toolbox.

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

カテゴリ

製品

質問済み:

2019 年 8 月 5 日

編集済み:

2019 年 8 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by