MATLAB Answers

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

8 views (last 30 days)
Cantor Set
Cantor Set on 18 Feb 2020
Commented: Matt J on 18 Feb 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

  0 Comments

Sign in to comment.

Accepted Answer

Matt J
Matt J on 18 Feb 2020
Edited: Matt J on 18 Feb 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 Comments

Matt J
Matt J on 18 Feb 2020
You are quite welcome, but please Accept-click the answer if we've reached a solution for you.

Sign in to comment.

More Answers (0)

Sign in to answer this question.