Specific pseudo-randomised number generator.

I am currenlty running an experiment that reads from a text file that requires specific formatting.
I need to create a matrix composed to two colunms (e.g. 200 x 2). One column needs to consist of only 1's and -1's in a pseudorandomised order with no more than three repeats of the same number consequatively and each number to appear an equal amount of times. The other column needs to be composed of 0's and 1's, where the 1's match up with the 1's in the first column and the 0's match up with the -1's.
I was wondering if this would be simple to do in Matlab and/or if anyone could help me out?
Cheers.

 채택된 답변

Bruno Luong
Bruno Luong 2019년 9월 19일
편집: Bruno Luong 2019년 9월 19일

1 개 추천

n = 2*ones(2,50);
for k=1:2
p=randperm(50,32); % you might randomize 32 as well (must be even number in [0,50]);
n(k,p)=repmat([1 3],1,length(p)/2);
end
A=repelem(repmat([-1 0; 1 1],50,1),n(:),1)

댓글 수: 4

Matt W
Matt W 2019년 9월 19일
Hi Bruno,
Thanks for this, works great! What is the exact purpose of 32 and what would be the effect if it were to be changed to a bigger or smaller number?
Thanks again.
Bruno Luong
Bruno Luong 2019년 9월 19일
편집: Bruno Luong 2019년 9월 19일
32=2*16
Meaning I want to generate 16 sequence of 1-length, 16 sequences of 3-length and then 18 (=50-32) sequences of 2-length (so the total length of all sequences is 100).
If you reduce the parameter 32 you'll have more sequences of length 2 than 1 and 3. And vice versa.
Matt W
Matt W 2019년 9월 19일
Great, thank you. Last thing; I then assume that if I require additional rows (e.g. 400 rather than 200) I would change the 50 to 100 and would need to adjust the parameter 32 accordingly e.g. 64 or 66?!
Thanks.
Bruno Luong
Bruno Luong 2019년 9월 19일
Correct.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Random Number Generation에 대해 자세히 알아보기

질문:

2019년 9월 18일

댓글:

2019년 9월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by