Randomly selecting a number from different arrays

Hello,
I am trying to randomly select numbers from 3 different arrays without having them side by side or repeated. For example, I have 3 arrays like below and pick 1 random number from them with the code below.
A = [11 11 11 12 12 12 13 13]
B = [21 21 21 22 22 22 23 23]
C = [31 31 31 32 32 32 33 33]
I would need 18 string of numbers and would there a way to pick 1 random number from A, and for the next number it would come from only B or C? (and after that, the number would only come from the other two list.)
Many thanks!

Réponses (1)

A = [11 11 11 12 12 12 13 13];
B = [21 21 21 22 22 22 23 23];
C = [31 31 31 32 32 32 33 33];
M=[A;B;C];
I=mod(cumsum(randi(2,1,18)),3)+1;
J=randi(length(A),size(I));
selection=M( sub2ind(size(M),I,J) )
selection = 1×18
22 33 12 21 11 23 12 21 12 33 11 31 12 22 31 11 33 22

8 commentaires

Thanks so much Matt! However, when I tried implementing this code to a longer array with more diverse numbers, I noticed there were some repeats in the selection of the numbers. Would there be a way to resolve this?
Matt J
Matt J le 14 Oct 2021
I don't know what you did...
Apologizes. I have changed the amount of numbers that was in the arrays and changed the picking 18 strings to 48.
A = [11 11 11 11 12 12 12 12 12 12 12 12 13 13 13]; %Increased the numbers
B = [21 21 21 21 22 22 22 22 22 22 22 22 23 23 23];
C = [31 31 31 31 32 32 32 32 32 32 32 32 33 33 33];
M=[A;B;C];
I=mod(cumsum(randi(2,1,48)),3)+1; %I have only modified this to 48 because I wanted more strings
J=randi(length(A),size(I));
selection=M( sub2ind(size(M),I,J) )
Matt J
Matt J le 15 Oct 2021
Modifié(e) : Matt J le 15 Oct 2021
The following test shows no repetitions after running 100K times:
A = [11 11 11 11 12 12 12 12 12 12 12 12 13 13 13]; %Increased the numbers
B = [21 21 21 21 22 22 22 22 22 22 22 22 23 23 23];
C = [31 31 31 31 32 32 32 32 32 32 32 32 33 33 33];
M=[A;B;C];
repeats=0;
for i=1:100000
I=mod(cumsum(randi(2,1,48)),3)+1; %I have only modified this to 48 because I wanted more strings
J=randi(length(A),size(I));
selection=M( sub2ind(size(M),I,J) );
repeats=repeats + nnz(diff(selection)==0);
end
repeats
repeats = 0
Quite odd. Even though the test indeed says there are no repeats, when I look at the results after running the code I some numbers are picked out more (some less) from the given array.
A = [11 11 11 11 12 12 12 12 12 12 12 12 13 13 13]; %Increased the numbers
B = [21 21 21 21 22 22 22 22 22 22 22 22 23 23 23];
C = [31 31 31 31 32 32 32 32 32 32 32 32 33 33 33];
M=[A;B;C];
I=mod(cumsum(randi(2,1,45)),3)+1; %I have only modified this to 48 because I wanted more strings
J=randi(length(A),size(I));
selection=M( sub2ind(size(M),I,J) );
selection 1X45
31 22 32 21 32 21 12 21 31 11 21 13 32 23 13 22 12 31 22 31 12 23 31 11 31 13 23 32 12 22 12 22 32 23 12 32 22 12 32 22 12 32 22 33 23
For example, even though there are only four 31 in the C array, in the soultion there are 6. And there are three 33s in the C array, but the solution holds only one 33. Would there be a way to pick the numbers out just as much as the arrays? I apologize for the inconvenience and I really appreciate your help!!
Maybe this is what you want?
A = [11 11 11 11 12 12 12 12 12 12 12 12 13 13 13]; %Increased the numbers
B = [21 21 21 21 22 22 22 22 22 22 22 22 23 23 23];
C = [31 31 31 31 32 32 32 32 32 32 32 32 33 33 33];
n=numel(A);
M=[A(randperm(n));B(randperm(n));C(randperm(n))];
I=mod(cumsum(randi(2,1,3*n)),3)+1; %I have only modified this to 48 because I wanted more strings
J=repelem(1:n,3,1);
selection=M( sub2ind(size(M),I(:),J(:)) ).'
selection = 1×45
22 32 22 33 21 33 22 13 22 32 11 32 22 33 12 22 31 11 32 12 21 32 12 22 12 31 21 12 22 12
It is much closer to what I was expecting, but it seems like I keep getting different amount of numbers from the arrays. I will look into it a little more! Appreciate the help SO much!
Matt J
Matt J le 19 Oct 2021
Modifié(e) : Matt J le 19 Oct 2021
You can't be guaranteed to get the same amount of numbers, because the rule you have specified in your post does not call for a pure permutation of the values in A,B,C. If you wanted a pure permutation you would simply do,
M=[A;B;C]
selection=M(randperm(numel(M)))
The rule you have specified, however, is that none of A,B,C can be selected twice in succession. This means, for example, that the sequence A,B,A,B,A,B,... with no occurence at all of C is hypothetically possible.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Produits

Modifié(e) :

le 19 Oct 2021

Community Treasure Hunt

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

Start Hunting!

Translated by