フィルターのクリア

How can i control an event based on a percentage?

2 ビュー (過去 30 日間)
Francesco Maria
Francesco Maria 2023 年 3 月 3 日
回答済み: Jasvin 2023 年 3 月 6 日
Hi, i have an event that will happen with a certain probability. I want to check this event, and know if the event is succeded or not. For example: i have a function that returns a percentage of this particular event happening. How can i check if the event will happen or not? Can randsample be a good idea, or there is something more intuitive and simple?

回答 (1 件)

Jasvin
Jasvin 2023 年 3 月 6 日
If you have the probability of the event happening and you want to get the data entries/samples that would successfully result in the event happening, randsample seems like an overcomplication of the problem.
Boiling down your problem as,
Generate 0 and 1 according to the fixed probability which you already have. So, in the output wherever you have 1s, that is the event happening and wherever you have 0s, that is the event not happening. And if you have a population vector for these events, then you can use this binary vector as the index for selecting only those entries where the event happened.
And the code for this is as simple as,
rand(1, n) >= x
where, x = Probability of event happening and n = Number of trials of the event (or the size of your population vector)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by