pseudorandom sequence without consecutively repeating numbers

조회 수: 2 (최근 30일)
alice
alice 2017년 10월 10일
댓글: dila suay 2020년 9월 13일
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
James Tursa
James Tursa 2017년 10월 13일
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?

댓글을 달려면 로그인하십시오.

답변 (2개)

Jyotish Robin
Jyotish Robin 2017년 10월 13일
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
alice
alice 2017년 10월 13일
편집: alice 2017년 10월 13일
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...

댓글을 달려면 로그인하십시오.


James Tursa
James Tursa 2017년 10월 13일
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
dila suay
dila suay 2020년 9월 13일
Hello, how can I modify this code that no more than 3 consecutive repetition will occur?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by