How can I ensure no repeated adjecent values in a vector?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello I've created a code in wich I generate an array of 52 values randomly of 1s and 2s with an 85% prob for 1s and 15% for 2s (approximately). Now I need to reorganize them in a way were I wont have 2s adjecent and at least two 1s before the next 2. to give you a better idea its this sequence: "1 1 1 1 2 1 1 1 1 2 1 1 1 2 1 1 1 1 2 1 1 2.....". This is to create an audio output for 1s and 2s. That means if x=1 the play A and X=2 Play B. Thats the idea behind it but I really need help with the second part.
My code is:
trials=[]
for i=1:5000;
test = rand_trial(0.85,52); % rand_trial is a function I made apart works fine
tbl = tabulate(test);
if tbl(1,2) == 44; % This is ne number of 1s I need for the trial
trials = test;
break
end
end
I would be most grateful for any help or advice.
Thanks in Advance
댓글 수: 0
채택된 답변
Guillaume
2018년 6월 25일
This is how I'd do it:
while true %run until we get an acceptable sequence. break will quit the loop
test = (randperm(52) > 44) + 1; %create a random permutation of 44 1s and 8 2s
if min(diff(find(test == 2))) > 3 %minimum distance between 2s is 3
break;
end
end
Note: rather than creating a distribution that has an 85% probability of 1s and then checking that there are exactly 44 1s, I just directly create a random permutation of 44 1s out of 52. In my opinion, it makes more sense.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!