Generating sequences with minimal overlap

Hi there,
I aiming to run a relatively basic sequence learning task, which has a mixture of practised and random sequences.
The practise sequences is as follows: 312 423 214 134
I now need to generate a number of (e.g., 10) random sequences of the same length that include as little overlap as possible e.g., cannot include the same run of 4 numbers anywhere.
I was therefore wondering if there was a way to automise this using some custom code or function.
Any help would be much appreciated.

 Accepted Answer

hello
maybe this ...
the result appears in store_str (in your command window)
% init
edges = (1:1:9);
k = 0;
store = [];
samples = 12; % generate a 12 numbers long array like 312 423 214 134
%% main loop
while 1
R = randi([1,9],1,samples); % generate a 12 numbers long array (random integers between 1 and 9)
N = max(histc(R,edges)); % N = max of (qty of) identical numbers that appears in R
if N<4 % keep it
store = [store; R];
k = k +1 ;
end
if k>= 10 % job done
break
end
end
% want "store" output as a char array ?
store_str = num2str(store);
% some "smart" deblanking to have 4 blocks sperated with one blank column
[m,n] = size(store_str);
nb_blanks = (n-samples)/(samples-1); % 2 blanks separation between individual numbers
ind = (1:nb_blanks+1:n); % index of columns containing numbers
ind2 = ind(3:3:end-1)+1; % add some blank column every 3 numbers
ind = sort([ind ind2]); % concat and sort
store_str = store_str(:,ind) % finally ...

4 Comments

Thank you that works great! Do you know if there is a way to prevent repeted numbers appearing in the sequence, e.g., 122 would not be acceptable.
Thanks
this is another way to do it with randperm
it has the benefit of creating non repeating random numbers but we can do it only for 9 numbers and not 12numbers. So the trick here is to complement the first 9 numbers with 3 numbers picked again randomly among the first 9 numbers.
therefore we can have 3 duplicates and not 4 by design. And those duplicates happen only N = 2 by design again
for example :
'863 245 179 238'
'378 516 294 985'
'185 369 742 967'
'597 136 248 576'
'562 193 847 439'
'527 134 986 452'
'973 156 824 876'
'823 615 479 275'
'672 843 519 163'
'594 236 187 149'
% init
edges = (1:1:9);
k = 0;
store = [];
samples = 12;
%% main loop
for ci =1:10
out = randperm(9); % random numbers with no duplicates => we have the first 9 unique numbers
% we need to pick randomly 3 of them that are not equal to the 9th to avoid
% repetition => create a vector of 3 indexes that are random integers
% between 1 and 8
ind = randperm(8,samples-9); % this is it
out = [out out(ind)]; % now we have 9 + 3 random numbers and we know that in the worst case those last 3 could be a repetition of the first 9
% let's check how many are identical
N = max(histc(out,edges)) % N = max of (qty of) identical numbers that appears in R
if N<4 % keep it
store = [store; out];
k = k +1 ;
end
end
% want "store" output as a char array ?
store_str = num2str(store);
% some "smart" deblanking to have 4 blocks sperated with one blank column
[m,n] = size(store_str);
nb_blanks = (n-samples)/(samples-1); % 2 blanks separation between individual numbers
ind = (1:nb_blanks+1:n); % index of columns containing numbers
ind2 = ind(3:3:end-1)+1; % add some blank column every 3 numbers
ind = sort([ind ind2]); % concat and sort
store_str = store_str(:,ind) % finally ...
Thank you so much! Final Q, is there a way to edit the script so the numbers are only between 1-4. I have had a go myself but it seems to get stuck towards the end and I can't seem to fix it.
ok - this is doable
in the man type I also implemented a check to avoid the repeted numbers appearing in the sequence (like 122)
first code modified :
some results :
'132 142 432 314'
'121 432 412 343'
'421 413 213 432'
'431 342 124 132'
'414 314 232 132'
'213 143 241 342'
'241 324 213 134'
'231 214 231 434'
'143 421 232 413'
'132 421 423 143'
% init
edges = (1:1:4);
k = 0;
store = [];
samples = 12;
%% main loop
while 1
out = randi([1,4],1,samples); % generate a 12 numbers long array (random integers between 1 and 9)
N = max(histc(out,edges)); % N = max of (qty of) identical numbers that appears in R
if N<4 % keep it
% check no contiguous numbers
d= diff(out);
nocn = find(abs(d)<1); % search for zero values
if isempty(nocn) % keep it
store = [store; out];
k = k +1 ;
N
end
end
if k>= 10 % job done
break
end
end
% want "store" output as a char array ?
store_str = num2str(store);
% some "smart" deblanking to have 4 blocks sperated with one blank column
[m,n] = size(store_str);
nb_blanks = (n-samples)/(samples-1); % 2 blanks separation between individual numbers
ind = (1:nb_blanks+1:n); % index of columns containing numbers
ind2 = ind(3:3:end-1)+1; % add some blank column every 3 numbers
ind = sort([ind ind2]); % concat and sort
store_str = store_str(:,ind) % finally ...
second code
results
'342 134 121 324'
'132 424 312 413'
'142 314 321 324'
'423 132 413 124'
'243 134 213 241'
'214 313 242 413'
'123 421 431 423'
'423 134 124 231'
'412 324 131 234'
'124 321 432 314'
% init
edges = (1:1:4);
k = 0;
store = [];
samples = 12;
%% main loop
while 1
out = [randperm(4) randperm(4) randperm(4)];
% let's check how many are identical
N = max(histc(out,edges)); % N = max of (qty of) identical numbers that appears in R
if N<4 % keep it
% check no contiguous numbers
d= diff(out);
nocn = find(abs(d)<1); % search for zero values
if isempty(nocn) % keep it
store = [store; out];
k = k +1 ;
N
end
end
if k>= 10 % job done
break
end
end
% want "store" output as a char array ?
store_str = num2str(store);
% some "smart" deblanking to have 4 blocks sperated with one blank column
[m,n] = size(store_str);
nb_blanks = (n-samples)/(samples-1); % 2 blanks separation between individual numbers
ind = (1:nb_blanks+1:n); % index of columns containing numbers
ind2 = ind(3:3:end-1)+1; % add some blank column every 3 numbers
ind = sort([ind ind2]); % concat and sort
store_str = store_str(:,ind) % finally ...

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 9 Nov 2022

Commented:

on 10 Nov 2022

Community Treasure Hunt

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

Start Hunting!