correct use of randsrc function

6 ビュー (過去 30 日間)
Akriti Raj
Akriti Raj 2021 年 6 月 9 日
回答済み: Walter Roberson 2021 年 6 月 10 日
out = randsrc(1,1,[-3 -1 1 3; 0.1 0.4 0.4 0.1])
Error: Undefined function 'randsrc' for input arguments of type 'double'.
Aim: I want to display a random number from the set -3, -1, 1, 3 each with respective probabilities 0.1, 0.4, 0.4, 0.1

回答 (2 件)

Scott MacKenzie
Scott MacKenzie 2021 年 6 月 9 日
編集済み: Scott MacKenzie 2021 年 6 月 9 日
There are lots of ways to do this, I suppose. Here's what I put together -- with a pesky twist. The weighting is built-in to the set. Just pull a value from set at random and the weighting is a natural outcome.
% define set
set = [-3 -1 -1 -1 -1 1 1 1 1 3];
% get random value from set (with built-in weighting)
set(randi([1 length(set)],1))
  1 件のコメント
Akriti Raj
Akriti Raj 2021 年 6 月 10 日
Thanks Scott

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


Walter Roberson
Walter Roberson 2021 年 6 月 10 日

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by