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!
채택된 답변
추가 답변 (1개)
James Tursa
2018년 4월 23일
편집: James Tursa
2018년 4월 23일
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
AlisonS
2018년 4월 23일
James Tursa
2018년 4월 23일
편집: James Tursa
2018년 4월 23일
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
2018년 4월 23일
James Tursa
2018년 4월 24일
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
카테고리
도움말 센터 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!