Creating random order of trials (numbers in a matrix) with restrictions
7 views (last 30 days)
Hi! I am new to MATLAB and I am struggling with creating a matrix with numbers which would indicate the different trials of an experiment. The experiment consists of 5 blocks with 50 trials in each block (10 different trials, each repeated 5 times per block). I would like to always start with a different first trial (so not start for instance with trial index 3 in two blocks) and not to repeat the same trial more than once in any given run (so for instance 5 6 2 2 9 ... is ok, but not 5 6 2 2 2 9). I only have the core of the structure - just the randomization, but not the two constraints.
???????? How to make the array re-do itself so that the first number will definitely be different?
Joseph Cheng on 29 Apr 2015
Well initial thoughts are to generate the initial matrix using
for ind = 1:5
to generate columns 2 to 50. The chances of 3 repeats are low but not infeasible. Then attach column 1 using the randperm(10,5) to get non repeating numbers.
Then go through and detect of groups of consecutive numbers. This can be done through something like this
and then look for the pattern of [1 0 -1] which would mean there would be 3 consecutive same numbers. more consecutive numbers would be more zeros between the 1 and -1. Then knowing the index locations substitute these numbers with another random set of numbers and recheck/repeat till no consecutive numbers are repeated >2 times in a row.
pfb on 29 Apr 2015
So you need a 5x50 matrix of numbers between 1 and 10, such that:
- all the numbers in the first column are different - no number can appear more than two consecutive times in a row
I think I have it. Since it is not a lot of numbers, I did this in a perhaps trivial way, using 2 loops and some parsing
A = zeros(5,50);
pf=0; % previous element in the 1st column. It is 0 at the beginning
for r =1:5
% first trial
% this takes care of the constraint in 1st column
% this should avoid that more than 2 consecutive elements appear in
% each row