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!