Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Generating sequence of 0s and 1s with no repetition

Subject: Generating sequence of 0s and 1s with no repetition

From: Vitor

Date: 22 Jul, 2013 17:32:10

Message: 1 of 4

Hello.
I need to generate a sequence of 100 numbers composed of 0s and 1s only.
But I must guarantee that no more that 3 repetitions occur.
For xample:
1 2 2 1 2 2 1 1 1 2 1... its ok
1 2 1 2 2 2 2 1 it's not.
I used randi for the generation of the sequence. But i'm a bit stuuck with the remaining part. I'm sure there is an efficient way of achieving this.
Thanks for any help.

Subject: Generating sequence of 0s and 1s with no repetition

From: Alan_Weiss

Date: 22 Jul, 2013 17:59:59

Message: 2 of 4

On 7/22/2013 1:32 PM, Vitor wrote:
> Hello.
> I need to generate a sequence of 100 numbers composed of 0s and 1s only.
> But I must guarantee that no more that 3 repetitions occur.
> For xample:
> 1 2 2 1 2 2 1 1 1 2 1... its ok
> 1 2 1 2 2 2 2 1 it's not.
> I used randi for the generation of the sequence. But i'm a bit stuuck
> with the remaining part. I'm sure there is an efficient way of
> achieving this.
> Thanks for any help.

I guess I would alternately sample between {0,00,000} and {1,11,111}
until you get to 100 digits.

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: Generating sequence of 0s and 1s with no repetition

From: Alan_Weiss

Date: 22 Jul, 2013 18:30:08

Message: 3 of 4

On 7/22/2013 1:59 PM, Alan_Weiss wrote:
> On 7/22/2013 1:32 PM, Vitor wrote:
>> Hello.
>> I need to generate a sequence of 100 numbers composed of 0s and 1s only.
>> But I must guarantee that no more that 3 repetitions occur.
>> For xample:
>> 1 2 2 1 2 2 1 1 1 2 1... its ok
>> 1 2 1 2 2 2 2 1 it's not.
>> I used randi for the generation of the sequence. But i'm a bit stuuck
>> with the remaining part. I'm sure there is an efficient way of
>> achieving this.
>> Thanks for any help.
>
> I guess I would alternately sample between {0,00,000} and {1,11,111}
> until you get to 100 digits.
>
> Alan Weiss
> MATLAB mathematical toolbox documentation

I was curious, so played with a script. This uses randsample from
Statistics Toolbox, but you could cobble together your own sampling
function easily:

z = {'0','00','000'};
p = {'1','11','111'};
rz = randsample(z,50,true); % gets at least 50 digits
rp = randsample(p,50,true);
r = [rp;rz]; % a 2-by-50 array
rr = r(:)'; % a 1-by-100 array of alternating samples
rout = strjoin(rr,''); % all the digits
rout = rout(1:100); % the first 100 digits

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: Generating sequence of 0s and 1s with no repetition

From: Chris

Date: 22 Jul, 2013 18:47:18

Message: 4 of 4

Just beware that if you are simulating a stochastic process then this may lead to unwanted results, that is, I don't know if the likelihood of '0','00', and '000' should be treated as equal.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us