How can I generate a randomized vector of repeating numbers with given conditions?

I'm trying to generate a vector of pseudo-random numbers that contains 1:11 repeated 8 times. The vector is broken up into blocks of 8. No number within each block can be repeated and each number has to occur at each position within the block once (i.e the number 4 has to occur first-eighth in eight different blocks). I've gone through a ton of iterations to do this and have attached the most recent attempt. The problem here is that by the end of the loop, the only possible numbers remaining are ones that have already been added within the current block of 8 numbers, so it becomes an endless while loop. Any help would be much appreciated!
TargTest=nan(8,11);%M by N array to indicate if Nth target was tested at Mth location
TestSessionTargs=[]; %Test Session Target order
while length(TestSessionTargs)<numel(TargTest)
TestBlock=nan(1,8);
for B=1:length(TestBlock)
PossibleTargs=find(isnan(TargTest(B,:))==1);
if length(PossibleTargs)~=1
F=randsample(PossibleTargs,1);
while ismember(F,TestBlock)
F=randsample(PossibleTargs,1);
end
else
F=PossibleTargs;
end
TestBlock(B)=F
TargTest(B,F)=1;
end
TestSessionTargs=[TestSessionTargs TestBlock];
end

7 Comments

Are the blocks "sliding" blocks? Could a 7 from one block happen to be at the end of the block, and a 7 happen to be at the beginning of the next block?
Below is a check that I use on the vector to tell me if that number has been tested at the position in the block and the block number it is located in
y2=reshape(TestSessionTargs,8,11);
TestedVal=nan(size(y2)); %Each column represents target location. Each row represents Nth trial in Test block
TestedValLoc=cell(size(y2));
for M=1:size(y2,1)
for N=1:size(y2,2)
if isnan(TestedVal(M,y2(M,N)))
TestedVal(M,y2(M,N))=1;
TestedValLoc{M,y2(M,N)}=[TestedValLoc{M,y2(M,N)} N];
else
TestedVal(M,y2(M,N))=TestedVal(M,y2(M,N))+1; %Tells us how many times location was tested
TestedValLoc{M,y2(M,N)}=[TestedValLoc{M,y2(M,N)} N]; %Tells us block this location was tested in
end
end
end
If you have blocks of 8, but you have 11 different values 1:11, then how can each of the 11 different values occur once in each position?
Or is the idea that a pseudo-random subset of 8 of the 11 will be chosen, and after that the conditions must be filled for that subset?
Yes, that can happen
[1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1] is an example of two consecutive valid blocks where 8 values occur at different positions within the blocks but do not repeat within the same block.
I have 11 different values repeated 8 times, so that's 88 values broken up into blocks of 8. The 88 values need to be arranged in the way that I mentioned
Another attempt I have made:
TargTest=nan(8,11);%M by N array to indicate if Nth target was tested at Mth location
TestSessionTargs=[]; %Test Session Target order
%While loop will go through TargTest M*N times row-wise and choose random
%index that ~isnan once it chooses that index in TargTest, places 1 as
%place holder to say location has been tested at particular time.
while length(TestSessionTargs)<numel(TargTest)
TestBlock=nan(1,8);
for B=1:size(TargTest,1)
F=randi(11,1);
while ~isnan(TargTest(B,F))||ismember(F,TestBlock)
F=randi(11,1);
end
TestBlock(B)=F;
TargTest(B,F)=1;
end
TestSessionTargs=[TestSessionTargs TestBlock];
end

Sign in to comment.

 Accepted Answer

If each symbol in the current symbol set must occur exactly once in each position over the block of 8, then you should research Latin Squares of Order 8. See here

3 Comments

Thanks, so because I'm essentially doing an 8 by 11, because it's not square it's going to be impossible?
Ah I looked at the latin square. the problem here is that from what I see, everything is in order. Another stipulation is that we do want to keep randomization, so rather than saying [1 2 3 4 5 6 7 8], it should be something more random like [7 2 3 6 1 5 8 4]
This works! There's a file exchange for a latin square function latsq that also provides randomized latin squares to solve the problem.
Thank you for the direction!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!