mardi 30 juin 2015

Create random binary sequences with last and first element equal to 1

I use rand(1,N)<=0.5in order to create binary sequences with 1s, and 0s.

N can take any value from 0 and above.

What I want to achieve is to have the same functionality as above but secure that always first and last elements will be 1.

For example

For N=0 --> empty array

For N=1 -->[ 1 ]

For N=2 -->[ 1 1 ]

For N=3 --> [1 0 1] or [1 1 1]

For N=4 --> [1 0 0 1] or [1 0 1 1] or [1 1 0 1] or [1 1 1 1]

and you see the logic?? Is there any automated way to achieve this?? For N=2 and above I guess [1 rand(1,N-2)<=0.5 1] works, but I want something that also works for N=0 and N=1;

Thanks




Aucun commentaire:

Enregistrer un commentaire