Asked by Dushyant Dhundara
on 19 Apr 2016

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

Answer by Jon
on 19 Apr 2016

Edited by 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.

Dushyant Dhundara
on 19 Apr 2016

Sign in to comment.

Answer by 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

Sign in to comment.

Answer by 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

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 10 Comments

## Azzi Abdelmalek (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/279915-in-a-sequence-of-0-and-1-how-to-make-sure-that-no-same-values-appear-more-than-2-times-consecutivel#comment_360124

## Meade (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/279915-in-a-sequence-of-0-and-1-how-to-make-sure-that-no-same-values-appear-more-than-2-times-consecutivel#comment_360131

## Dushyant Dhundara (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/279915-in-a-sequence-of-0-and-1-how-to-make-sure-that-no-same-values-appear-more-than-2-times-consecutivel#comment_360133

## Dushyant Dhundara (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/279915-in-a-sequence-of-0-and-1-how-to-make-sure-that-no-same-values-appear-more-than-2-times-consecutivel#comment_360137

## Image Analyst (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/279915-in-a-sequence-of-0-and-1-how-to-make-sure-that-no-same-values-appear-more-than-2-times-consecutivel#comment_360141

## Dushyant Dhundara (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/279915-in-a-sequence-of-0-and-1-how-to-make-sure-that-no-same-values-appear-more-than-2-times-consecutivel#comment_360144

## Meade (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/279915-in-a-sequence-of-0-and-1-how-to-make-sure-that-no-same-values-appear-more-than-2-times-consecutivel#comment_360386

## Dushyant Dhundara (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/279915-in-a-sequence-of-0-and-1-how-to-make-sure-that-no-same-values-appear-more-than-2-times-consecutivel#comment_360403

## Image Analyst (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/279915-in-a-sequence-of-0-and-1-how-to-make-sure-that-no-same-values-appear-more-than-2-times-consecutivel#comment_360428

## John BG (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/279915-in-a-sequence-of-0-and-1-how-to-make-sure-that-no-same-values-appear-more-than-2-times-consecutivel#comment_360437

Sign in to comment.