Generate N random number from weighted groups

Hi,
I am wanting to generate numbers from weighted groups, for example this could be a set of 10 numbers from the ranges 0:50 and 100:150.
I have tried
>> randsample([0:50, 100:150],10,true,[0.3 0.7])
But cannot seem to get the randsample function working for weighted groups, any help would be appreciated
Thanks

답변 (2개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 12월 18일
편집: KALYAN ACHARJYA 2020년 12월 18일

0 개 추천

w, of the same length as the vector population
randsample([0:50, 100:150],10,true,rand(1,102));
%.......................................102 vector population length (example)
Steven Lord
Steven Lord 2020년 12월 18일
groupProbs = [0.3 0.7];
groupVal = rand(10, 1);
whichGroup = groupVal <= groupProbs(1); % for more groups, use discretize
results = zeros(size(whichGroup));
results(whichGroup == 1) = randi([0 50], nnz(whichGroup == 1), 1); % For more groups, for loop
results(whichGroup == 0) = randi([100 150], nnz(whichGroup == 0), 1);
finalResults = table(groupVal, results)
finalResults = 10x2 table
groupVal results ________ _______ 0.93344 114 0.64081 143 0.70705 132 0.16729 1 0.27142 8 0.31064 125 0.55051 148 0.34207 116 0.33953 120 0.070558 49

질문:

2020년 12월 18일

답변:

2020년 12월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by