pseudorandom sequence without consecutively repeating numbers

2 次查看(过去 30 天)
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;

类别

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