Advanced use of randi(x) and other random number generating functions

조회 수: 2 (최근 30일)
David
David 2014년 4월 18일
댓글: David 2014년 4월 30일
Is it possible to get the randi(x) function to work with a specifically chosen set of numbers, such as 1 through 9 except 6?
From what I can tell, the function description doesn't make it seem possible. Is there a creative way around this? What about using other random number generating functions?

채택된 답변

Sean de Wolski
Sean de Wolski 2014년 4월 18일
There's nothing builtin do exclude certain numbers. Of course this is accomplished with a little bit of manipulation:
x=randi(8,[5 5])
x(x>=6) = x(x>=6)+1
randperm might be your friend as well.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 18일
your_range=[1:5 7:9]
[~,ii]=sort(rand(1,numel(your_range)))
out=your_range(ii)
  댓글 수: 1
David
David 2014년 4월 30일
The sort(x) function became very important later. Thank you for pointing it out!

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

카테고리

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