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

조회 수: 24 (최근 30일)
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

채택된 답변

Matt J
Matt J 2020년 2월 18일
편집: Matt J 2020년 2월 18일
No loops required,
N=10; b=3; M=5; %example data
list=setdiff(1:N,b);
selection=list(randperm(N-1,M)) %result
  댓글 수: 2
Matt J
Matt J 2020년 2월 18일
You are quite welcome, but please Accept-click the answer if we've reached a solution for you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by