pseudorandom sequence without consecutively repeating numbers

19 ビュー (過去 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 日
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?

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by