pseudorandom sequence without consecutively repeating numbers

3 Ansichten (letzte 30 Tage)
alice
alice am 10 Okt. 2017
Kommentiert: dila suay am 13 Sep. 2020
Hi everybody, I'm struggling with the creation of my experiment matrix. I have to present 4 different conditions (1,2,3,4) 30 times (tot number of trials 120). I want to randomly shuffle my conditions vector without repeating two times consecutively the same condition, given that each condition is repeated equally. Moreover I have a second vector that can be 1 or 0 and I need that each condition has the same number of 0 and 1. More specifically, I would like to repeat the matrix [1 2 3 4 1 2 3 4 ;1 1 1 1 0 0 0 0]15 times, so that 1, 2, 3 and 4 would all repeat 30 times without being consecutive. Example :
%Wrong pseudorandom [1 2 3 4 1 2 1 4 4 4 3 2 1 3 3 2 ; 1 0 1 0 0 0 1 0 1 1 0 1 0 0 1 1
%Right pseudorandom [1 2 3 4 1 2 1 4 3 4 3 2 1 3 3 2; 1 0 1 0 0 0 1 0 1 1 0 1 0 0 1 1]
Many thanks for any suggestions,
AB
  1 Kommentar
James Tursa
James Tursa am 13 Okt. 2017
Your example doesn't seem to match your words. The "Right" solution has four 1's, four 2's, five 3's, and three 4's in that first line. So there are not the same number of 1's, 2's, 3's, and 4's. Also, there are two 3's next to each other. Is this a typo?

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jyotish Robin
Jyotish Robin am 13 Okt. 2017
Hi Alice!
A possible approach would be as follows:
First, create a matrix of size 4 x 30. Each column is a random permutation of the numbers 1, 2, 3, 4.
[randperm(4)' randperm(4)' ..... randperm(4)'] % 30 times 'randperm' needs to be used.
Now, based on the entry of the last row in the first column randomly pick a column vector with a different first-row entry and make it the new second column. Repeat this for all columns except the last.
Now, you just have to concatenate all of the column vectors. (One way is to use the reshape functionality. https://www.mathworks.com/help/matlab/ref/reshape.html
Hope this helps!
Regards,
Jyotish
  1 Kommentar
alice
alice am 13 Okt. 2017
Bearbeitet: alice am 13 Okt. 2017
Many thanks, but I have already trid this solution... you can't avoid that sometimes the last item and first item of the next solution are the same...

Melden Sie sich an, um zu kommentieren.


James Tursa
James Tursa am 13 Okt. 2017
This code repeats the randperm( ) groups rather than letting the values be scattered in a more random fashion, but perhaps this will be good enough for your purposes. The fliplr( ) part keeps numbers from being consecutive.
nconditions = 4;
nrepeats = 30;
n = nconditions * nrepeats;
result = zeros(2,n);
result(1,1:nconditions) = randperm(nconditions);
for k = 1:nrepeats-1
m = k * nconditions;
r = randperm(nconditions);
if( r(1) == result(1,m) )
r = fliplr(r);
end
result(1,m+1:m+nconditions) = r;
end
p = randperm(n);
result(2,p(1:n/2)) = 1;
  1 Kommentar
dila suay
dila suay am 13 Sep. 2020
Hello, how can I modify this code that no more than 3 consecutive repetition will occur?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by