I’m a graduate student in biology that encountered a problem that I really struggled solving in my project. I solved it using an iterative approach (will describe more below) but was curious if there was a simpler or more elegant solutions to this.
I wanted to generate a sequence of numbers. Let’s assume we have vector v = 1:12
. I want to generate a sequence consisting of the vector v = 1:12
, with n
repetitions. The resulting sequence will be of length v * n
. Additionally, for each element of this sequence, I want to add a boolean value (I will explain more below).
I want this sequence to meet several conditions:
- In each repetitions of vector
v
, I want the elements1:12
to appear in random order. (this seems straightforward enough to me) - The boolean that is being assigned to each element of the sequence must be balanced within a repetition. That is, for a given
n
th repetition, I want there to be6 true
and6 false
. - Each value in vector
v
should have the same number oftrue
andfalse
across the entire sequence.
For example if n=10
, I have a sequence of length 120
. In this sequence, there should be 1
with 5 true
and 5 false
. 2
with 5 true
and 5 false
so on and so forth.
I tried to solve this problem by imposing conditions. If we get really unlucky when generating the initial repetitions, by n/2
repetition, we may have a value in v
that never gets a true
draw. Then, I need to place a condition that this value needs to be drawn for the remainder of the repetitions etc etc. You can apply similar logic for a value that has only been drawn once, twice, etc..
What would be the most easiest way to solve this? I have a code in MATLAB that works but would love to hear more solutions to this. It reminds me of a sudoku problem but honestly I have to admit I spent way too much time trying to figure this out so am reaching out for help. Thanks!