Calculate a randomized array with some constraints.
이전 댓글 표시
Hello, for a psychological experiment I have the following problem. I need to calculate an array containing 70 distinct numbers. From these 70 numbers are 20 pairs (so e.g. two 1s, two 2s, two 3s etc.), all other numbers are zeros. The constraint is now that all pairs shall have a fixed index step-size to each other (e.g. if the step-size=2 and the first 3 lays at index 2, than the second 3 must lay at index 4), but apart from that the array should be completely random. The step-size should be variable between n=2 to n=20. Whether two pairs overlap (e.g. that if the step-size=2, and the first pair lays at index 2 and 4, and the second pair lays at index 3 and 5), doesn’t matter. Also how long Matlab needs, to find such an Array does not really matter, as long it does not take more than an hour or so.
Does anyone have an idea how I can tell Matlab to output me such an array? This is what I tried so far:
myArray = [zeros(1,40),1:20,1:20]; % the array that shall be randomized,
% under the bespoken constraint.
lags = 2; % variable step-size
pairs = 20; % number of pairs
a=0;
while a==0
myArray = myArray(randperm(length(myArray))); % new randomization, new chance!
for i=1:pairs
tmp = find(myArray==i);
if tmp(1)+lags~=tmp(2); % if one pair has not the right index
%stepsize, interrupt for-loop.
break;
elseif i==pairs
a=1; % if all numbers are at their right
% place stop the while loop
end
end
However, I have to admit that this is totally bruteforce and in fact does never end (at least not within one hour ;-)).
I would be very very glad if someone could help me out with a smarter approach. Cheers.
댓글 수: 5
José-Luis
2014년 6월 24일
I am not sure I understand. Couldn't you just pick a random starting pair and then fill your array with a random step size?
brainybrown
2014년 6월 24일
José-Luis
2014년 6월 24일
I am sorry, but I am not sure I understand what you'd like to achieve. Could you provide an example of the desired result?
brainybrown
2014년 6월 24일
편집: brainybrown
2014년 6월 24일
brainybrown
2014년 6월 24일
편집: brainybrown
2014년 6월 24일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Psychology에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!