adding or subtracting depending on the number

3 ビュー (過去 30 日間)
jason
jason 2022 年 9 月 8 日
回答済み: Walter Roberson 2022 年 9 月 8 日
How do I make it so that if a random number is above a certain amount a number will be subtracted from a diffrent variable. But if the random number is below a certain amount it will be added to a variable.
  1 件のコメント
David Hill
David Hill 2022 年 9 月 8 日
What have you tried? Can you explain yourself better through a code example?

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

回答 (2 件)

Torsten
Torsten 2022 年 9 月 8 日
rng ("default")
amount = 0.5;
variable = 5;
number = 1;
r = rand;
if r >= amount
variable = variable - number;
else
variable = variable + number;
end
variable
variable = 4

Walter Roberson
Walter Roberson 2022 年 9 月 8 日
Variable = Variable + sign(Threshold - RandomNumber) * amount

カテゴリ

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

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by