In a sequence of 0 and 1, how to make sure that no same values appear more than 2 times consecutively?

3 views (last 30 days)
I am generating a sequence of equal number of 0's and 1's . A = [ 0 1 1 0 1 1 1 0 0 1 0 0] My goal is to not have either 0 or 1 be repeated more than 2 times consecutively. in the above array A, you can see that 1 1 1 appears so somehow I need to switch it around so that one of them becomes 0 and if there is something like 0 0 0 then switch one of them to 1. I have tried few algorithms which work for few iterations but then run into an error.
if true
nums = mod( reshape(randperm(1*12), 1, 12), 2)
%%%%%algorithm to check for a condition repeating more than 2 times and fix it
for i = 1:length(nums)-2
if nums(i+1)==nums(i) & nums(i+2) ==nums(i)
if nums(i) == 1
nums(i+2) = 0;
else
nums(i+2) = 1;
end
end
end end
Any help would be appreciated. Thanks
  10 Comments
John BG
John BG on 21 Apr 2016
To Image Analyst:
from the question 'My goal is to not have either 0 or 1 be repeated more than 2 times consecutively.'
yes, you can have 010 or 0110, but you cannot have 01110 ..

Sign in to comment.

Answers (3)

Jon
Jon on 19 Apr 2016
Edited: Jon on 19 Apr 2016
Just modify your loop a little bit:
for i = 3:length(nums)
if nums(i) == nums(i-1) && nums(i) == nums(i-2)
if nums(i) == 1
nums(i) = 0;
else
nums(i) = 1;
end
end
end
Oh, I see that you also require the same number of 1s and 0s. This doesn't ensure that. The easiest solution (though certainly not the fastest) is to add a check at the end that there are the same number; if not, reiterate until so.
  1 Comment
Dushyant Dhundara
Dushyant Dhundara on 19 Apr 2016
Edited: Dushyant Dhundara on 19 Apr 2016
Same issue as I described above, doing this it fixes the sequence but it increments the number of 1's or 0's. Please test this sequence: nums = [1 1 0 0 0 0 1 1 0 1 0 1] Can you show what kind of check you mean? Do i need a counter for that?

Sign in to comment.


John BG
John BG on 21 Apr 2016
Dushyant
the following generates sequences of random even length with your requirements of no longer than paired 1s or paired 0s bursts:
L=randi([0 10],1,1) % choose random amount of ones
header=randi([0 1],1,2) % choose random intitial 2 bits
S=zeros(1,2*L) % init sequence to all zeros
S(1)=header(1);S(2)=header(2) % cast header
k=3 % pointer to sequence bit to decide
n1s=L;n0s=L % meters counting how many ones and zeros left in each clip
while (n1s>0 && n0s>0)
if (S(k-2)==0 && S(k-1)==0 && n1s>0) % previous bits are 00
S(k)=1
n1s=n1s-1
k=k+1
end
if (S(k-2)==1 && S(k-1)==1 && n0s>0) % previous bits are 11
S(k)=0
n0s=n0s-1
k=k+1
end
if (S(k-2)==0 && S(k-1)==1) % previous bits are 01
S(k)=randi([0 1],1,1)
if (S(k)==1 && n1s>0)
n1s=n1s-1
end
if (S(k)==0 && n0s>0)
n0s=n0s-1
end
k=k+1
end
if (S(k-2)==1 && S(k-1)==0) % previous bits are 10
S(k)=randi([0 1],1,1)
if (S(k)==1 && Ln1s>0)
n1s=n1s-1
end
if (S(k)==0 && n0s>0)
n0s=n0s-1
end
k=k+1
end
end
If you find this answer of any help solving your question, please click on the thumbs-up vote link, or mark it as accepted answer
thanks in advance
John

Dushyant Dhundara
Dushyant Dhundara on 21 Apr 2016
I am not sure if its doing what I was looking for, but I appreciate your effort and help. I already found out a solution to the initial questions posted.
Thank you again

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!