Creating random order of trials (numbers in a matrix) with restrictions
29 views (last 30 days)
Show older comments
natalie222 on 29 Apr 2015
Commented: natalie222 on 2 May 2015
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?
yes... the math didn't add up on the repetition portion but in your clarification you were talking about consecutive trials. I'll come back if i think of anything that doesn't require lots of loops.
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
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!