Making random to work like randi
정보
This question is locked. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
이전 댓글 표시
I have this code
idx = randi(N_clusters,N_clusters,1);
but I want to use the random function to achieve the same output. I do not get into why I want to use the random function but it is important for my work flow. So then I tried
pd = makedist('DiscreteUniform','Lower',1,'Upper',N_clusters);
idx = random(pd,N_clusters,1);
But MATLAB gives an error that "DiscreteUniform" is not recognized. What is my best alternative usinf the random function, if I stll can.
댓글 수: 0
채택된 답변
추가 답변 (1개)
idx = floor( (N_clusters-1)*rand(N_clusters,1) )+1
댓글 수: 11
Snoopy
2025년 8월 4일
Matt J
2025년 8월 4일
Fine but random can imitate rand() using the 'Uniform' distribution selection (which does exist, unlike 'UniformDiscrete').
Snoopy
2025년 8월 4일
Matt J
2025년 8월 4일
Then replace the rand() function in my code with the random() function, invoked appropriately to create a uniform distribution.
Snoopy
2025년 8월 4일
Matt J
2025년 8월 4일
It should. We would have to see what you did.
Snoopy
2025년 8월 4일
Snoopy
2025년 8월 4일
These are the distributions that are available for the "makedist" command:
list = makedist
Snoopy
2025년 8월 4일
AI suggested
But that's not what I suggested to you earlier. I suggested 'Uniform'
N_clusters = 10;
rng('default');
pd = makedist('Uniform','Lower',1,'Upper',N_clusters+1);
idx = floor(random(pd,N_clusters,1))
This question is locked.
카테고리
도움말 센터 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!