How to generate random integer from 1 to N except some integers in between?

9 vues (au cours des 30 derniers jours)
Cantor Set
Cantor Set le 18 Fév 2020
Commenté : Matt J le 18 Fév 2020
Suppose I want to generate unrepeated M integers between 1 and N. but except an arbitrary integer b s.t 1<= b<=N
is there any one-line command or something or do I have to construct a loop?
Thanks

Réponse acceptée

Matt J
Matt J le 18 Fév 2020
Modifié(e) : Matt J le 18 Fév 2020
No loops required,
N=10; b=3; M=5; %example data
list=setdiff(1:N,b);
selection=list(randperm(N-1,M)) %result
  2 commentaires
Cantor Set
Cantor Set le 18 Fév 2020
Aha, thats smart :D
Thanks Matt
Matt J
Matt J le 18 Fév 2020
You are quite welcome, but please Accept-click the answer if we've reached a solution for you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices 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!

Translated by