generate unrepeated pairs from randi
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have two variables A and B. They are independently generated from randi with different range. For example, A=randi([0,5],1,5); B=randi([-3,3],1,5);
Now I assume the element in A along with the corresponding element in B consist a pair C, where C is of size 1×5. However, I don't want any pair in C is repeated. If repeated, just generate B again until the pair is unrepeated. Is there any simple code can realize this?
1 commentaire
Dyuman Joshi
le 2 Sep 2023
Did you mean that C is 2x5?
Also, to confirm, if there is a repeated pair, you just want to regenerate B?
Réponses (1)
Bruno Luong
le 2 Sep 2023
Modifié(e) : Bruno Luong
le 2 Sep 2023
a = 0:5;
b = -3:3;
n = 20; % 5 in your case
na = length(a);
nb = length(b);
[A,B] = ind2sub([na nb], randperm(na*nb, n));
A = a(A);
B = b(B);
C = [A(:) B(:)]
0 commentaires
Voir également
Catégories
En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!