Create a pseudo-random array with a set probability of similarity to an existing random array

I have created a random array of 32 trials each presenting a stimuli associated with a number 1-4. I want to make a second array also of numbers 1-4 where in 50% of the trials, the value in the second array equals the value in the first array. I also want to make sure that each stimuli (1-4) is being shown roughly the same number of times, i.e. if 16 trials have the same number in each array, 4 of them are stimuli 1, 4 are stimuli 2, 4 are stimuli 3, and 4 are stimuli 4. The other 16 trials do not have the same value in both arrays.
My code for the first array is
array1 = randi([1 4], 1, 32);
I am want array2(ii) == array1(ii) for 50% of the trials.
Thanks!

 Accepted Answer

With such constrained stimulus pairs it might be more straight-forward to generate exactly the pairs you want systematically and then randomize their order at the end, maybe like this:
matches4 = [1 1; 2 2; 3 3; 4 4];
matches16 = repmat(matches4,4,1); % Here are the 16 match trials.
nonmatches12 = [1 2; 1 3; 1 4; 2 1; 2 3; 2 4; 3 1; 3 2; 3 4; 4 1; 4 2; 4 3]; % 12 possible nonmatches
% Now make 4 extra nonmatches so that you will have sixteen total:
nonmatches4 = [1 2; 3 4; 4 1; 2 3]; % Not entirely sure which nonmatches you want to repeat.
stimuli = [matches16; nonmatches12; nonmatches4]; % stimulus pairs for 32 trials
stimuli = stimuli(randperm(size(stimuli, 1)), :); % randomize the order of the stimulus pairs

1 Comment

I was hoping to figure out a way to code it rather than specifically stating the trials but I think you might be right that it is easier to control in this way. Thank you!

Sign in to comment.

More Answers (1)

E.g.,
n = numel(array1); % total number of elements
n2 = floor(n/2); % half of them
x = randperm(n,n2); % random positions for half of them
array2 = array1; % start with exact copy
array2(x) = randi([1 4], 1, n2); % change half of them to new random numbers

4 Comments

Thank you.
Is there a way to ensure that only 50% of the trials are matches? This codes works to guarantee that at least half of the trials are matches but then there is the possibility that some of the other trials still end up matches because they are random. I want 50% of the trials to not be matches and 50% of the trials to be matches.
Also any thoughts on how to ensure that the matching trials are evenly distributed with each of the stimuli 1 - 4?

I don't have the time to write the code right now, but basically you would simply start with this array:

y = repmat([1;2;3;4],1,n2);

Then delete the one element from each column that matches array1(x). Then randomly draw one value from each column of the result to fill out array2(x). I cat get back to this later on this evening.

E.g., here is a somewhat brute force approach:

n = numel(array1); % total number of elements
n2 = floor(n/2); % half of them
x = randperm(n,n2); % random positions for half of them
array2 = array1; % start with exact copy
y = repmat([1;2;3;4],1,n2); % all of the possible indexes to use
y(array1(x) + (0:n2-1)*4) = []; % get rid of the indexes we don't want
array2(x) = y(ceil(rand(1,n2)*3) + (0:n2-1)*3); % random replace from leftover list

Sign in to comment.

Categories

Asked:

on 23 Apr 2018

Commented:

on 24 Apr 2018

Community Treasure Hunt

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

Start Hunting!