Generating sequences with minimal overlap

2 visualizaciones (últimos 30 días)
Matt
Matt el 9 de Nov. de 2022
Comentada: Mathieu NOE el 10 de Nov. de 2022
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.

Respuesta aceptada

Mathieu NOE
Mathieu NOE el 10 de Nov. de 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 comentarios
Matt
Matt el 10 de Nov. de 2022
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.
Mathieu NOE
Mathieu NOE el 10 de Nov. de 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 ...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by