Create a pseudo-random array with a set probability of similarity to an existing random array
Afficher commentaires plus anciens
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!
Réponse acceptée
Plus de réponses (1)
James Tursa
le 23 Avr 2018
Modifié(e) : James Tursa
le 23 Avr 2018
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 commentaires
AlisonS
le 23 Avr 2018
James Tursa
le 23 Avr 2018
Modifié(e) : James Tursa
le 23 Avr 2018
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.
AlisonS
le 23 Avr 2018
James Tursa
le 24 Avr 2018
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
Catégories
En savoir plus sur Multidimensional Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!