How to generate multiple random combinations of word pairs with specific restrictions on avoiding repetition?

1 vue (au cours des 30 derniers jours)
  1. I want to randomly combine word pairs such that I can generate an instruction - e.g., "Spin yellow rubber".
Actions={'spin',
'pick up',
'push',
'touch',
'shake',};
Objects={'yellow rubber',
'white rubber',
'pink ruler',
'blue ruler',
'orange folder',
'green folder',
'purple bag',
'brown bag',
'gold box',
'black box',
'red pencil',
'grey pencil'};
2. I'd like to expand this to generate a sequence of 4 instructions - e.g., "spin yellow rubber, pick up pink ruler, push orange folder, touch gold box'. I want to generate 192 sets of four-sequence instructions [192 x 4].
3. I don't want repetitions of the action AND the object within the sequence of 4 instructions. In my 192 x 4 instructions, I do not want the instruction (e.g., spin yellow rubber) to appear in two consecutive rows.

Réponse acceptée

Mathieu NOE
Mathieu NOE le 9 Mar 2021
@ Abi
this is the last iteration of the code
Actions={'spin',
'pick up',
'push',
'touch',
'shake',};
Objects={'yellow rubber',
'white rubber',
'pink ruler',
'blue ruler',
'orange folder',
'green folder',
'purple bag',
'brown bag',
'gold box',
'black box',
'red pencil',
'grey pencil'};
n = numel(Actions);
m = numel(Objects);
% create a vector of random integer values
% rows 1 / 3 / 5 / 7 : range : 1 to n
% rows 2 / 4 / 6 / 8 : range : 1 to m
candidates = 2e4; % nb of initial candidates
v1357 = randi([1 n],candidates,4);
v2468 = randi([1 m],candidates,4);
A = [v1357(:,1) v2468(:,1) v1357(:,2) v2468(:,2) v1357(:,3) v2468(:,3) v1357(:,4) v2468(:,4)];
k = 0;
q = 0;
C = 0;
B_old = zeros(1,8);
for ci = 1 : candidates
B = A(ci,:);
cond1 = (B(1) == B_old(1)) & (B(2) == B_old(2)); % test if first pair is identical to previous 1st pair
cond2 = (B(3) == B_old(3)) & (B(4) == B_old(4)); % idem for 2nd pair
cond3 = (B(5) == B_old(5)) & (B(6) == B_old(6)); % idem for 3rd pair
cond4 = (B(7) == B_old(7)) & (B(8) == B_old(8)); % idem for 4tg pair
test_previous = any([cond1 cond2 cond3 cond4]);
if (length(unique(B)) == 8) && (test_previous == 0) % keep only if all 8 elements are different and no duplicate pairs with previous iteration
k = k+1;
C(k,1:8) = B;
B_old = B;
end
end
% crop to first 192 rows
C = C(1:192,:);
for ck = 1:192
out = [Actions(C(ck,1)) {' '} Objects(C(ck,2)) {' '} Actions(C(ck,3)) {' '} Objects(C(ck,4)),...
{' '} Actions(C(ck,5)) {' '} Objects(C(ck,6)) {' '} Actions(C(ck,7)) {' '} Objects(C(ck,8))];
myWords{ck} = [out{:}];
end
FinalWords = myWords';

Plus de réponses (1)

Mathieu NOE
Mathieu NOE le 5 Mar 2021
Did I won the prize ?
Actions={'spin',
'pick up',
'push',
'touch',
'shake',};
Objects={'yellow rubber',
'white rubber',
'pink ruler',
'blue ruler',
'orange folder',
'green folder',
'purple bag',
'brown bag',
'gold box',
'black box',
'red pencil',
'grey pencil'};
n = numel(Actions);
m = numel(Objects);
% create a vector of random integer values
% 1st and 3rd row range : 1 to n
% 2nd and 4th row range : 1 to m
l = 500;
v1 = randi([1 n],l,1);
v3 = randi([1 n],l,1);
v2 = randi([1 m],l,1);
v4 = randi([1 m],l,1);
A = [v1 v2 v3 v4];
k = 0;
C = 0;
for ci = 1 : l
B = A(ci,:);
if length(unique(B)) == 4 % keep only if all 4 elements are different
k = k+1;
C(k,1:4) = B;
end
end
% crop to first 192 rows
C = C(1:192,:);
for ck = 1:192
out = [Actions(C(ck,1)) {' '} Objects(C(ck,2)) {' '} Actions(C(ck,3)) {' '} Objects(C(ck,4))];
myWords{ck} = [out{:}];
end
FinalWords = myWords';
  9 commentaires
Mathieu NOE
Mathieu NOE le 9 Mar 2021
@ Walter
I am not aware that randperm can generate a 2D array of random values with the guarantee to not have repeated values along both the row / column dimensions ?
it works only for 1D array as far as I know
tx for your help
Mathieu NOE
Mathieu NOE le 9 Mar 2021
also randperm does not allow me to generate more numbers than the max value of the range
so if my range is 1:4 , I get only 4 values which is not my intention. And looping is not an option as I loose the uniqueness of the random values.

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by