# Creating random order of trials (numbers in a matrix) with restrictions

11 visualizzazioni (ultimi 30 giorni)
natalie222 il 29 Apr 2015
Commentato: natalie222 il 2 Mag 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.
Many thanks!
A=zeros(5,50);
for i=1:5,
p=randi(10,1,50);
if p(1)==A(1:5,1)
???????? How to make the array re-do itself so that the first number will definitely be different?
else
end
A(i,:)=p;
end
##### 3 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
Joseph Cheng il 29 Apr 2015
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.

Accedi per commentare.

### Risposte (2)

Joseph Cheng il 29 Apr 2015
Well initial thoughts are to generate the initial matrix using
for ind = 1:5
A(ind,:)=mod(randperm(49,49),10)+1;
end
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
consecutive=diff(diff(A(ind,:))==0)
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.
##### 1 CommentoMostra NessunoNascondi Nessuno
natalie222 il 2 Mag 2015
Thanks a lot! :)

Accedi per commentare.

pfb il 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
t=randi(10);
% this takes care of the constraint in 1st column
while(t==pf)
t=randi(10);
end
A(r,1)=t;
pf=t;
cons=1;
for c=2:50
t=randi(10);
% this should avoid that more than 2 consecutive elements appear in
% each row
cons=cons+(t==A(r,c-1));
if(cons==3)
t=randi(10);
while(t==A(r,c-1))
t=randi(10);
end
cons=1;
end
A(r,c)=t;
end
end
##### 1 CommentoMostra NessunoNascondi Nessuno
natalie222 il 2 Mag 2015
Thank you! I made it work.

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!