Randomising a vector with respect to the probability of the outcome

2 ビュー (過去 30 日間)
Marek Galus
Marek Galus 2021 年 10 月 13 日
コメント済み: Marek Galus 2021 年 10 月 14 日
Hi, I have 60 trials which have two outcomes 0 and 1 with probabilities of 80% and 20%.
I want to randomise this 60x1 matrix in such a way that every 5 iterations, outcome 0 occurs 4 times and outcome 1 occurs once as they should.
I tried using ransrc but the 5x1 matrices that I get as an outcome very rarely match the desired probabilites.
  2 件のコメント
Mitchell Thurston
Mitchell Thurston 2021 年 10 月 13 日
This isn't a general case, but if I understand what you're wanting to do this'll do the job
result = zeros(60,1);
for i = 0:5:55
result(i + randi(5)) = 1;
end
Marek Galus
Marek Galus 2021 年 10 月 14 日
Thanks

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

採用された回答

Mohammad Alhashash
Mohammad Alhashash 2021 年 10 月 13 日
You can use the randperm function to build 12 basic 5-by-1 matrices and then map the values into 0 and 1 based one your probabilities.
Here what do I mean:
A = [];
m = 60; %length of the main matrix A
for i=1:5:m
a = randperm(5)';
a(a~=1) = 0; % this based on your probability, I used 20% 1 and 80% 0
A = [A;a];
end

その他の回答 (1 件)

Matt J
Matt J 2021 年 10 月 13 日
編集済み: Matt J 2021 年 10 月 13 日
I=randi(5,1,12);
J=1:12;
result=reshape( sparse(I,J,1,5,12) ,[],1);
full(result(:))
ans = 60×1
0 0 1 0 0 1 0 0 0 0

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by