Generating sequences with minimal overlap

1 view (last 30 days)
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.
  1 Comment
Torsten
Torsten on 9 Nov 2022
I don't understand your problem description.

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 10 Nov 2022
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
Mathieu NOE
Mathieu NOE on 10 Nov 2022
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)

Community Treasure Hunt

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

Start Hunting!