Info

This question is locked. 編集または回答するには再度開いてください。

Making random to work like randi

3 ビュー (過去 30 日間)
Snoopy
Snoopy 2025 年 8 月 4 日
Locked: Matt J 2025 年 8 月 4 日
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.

採用された回答

Paul
Paul 2025 年 8 月 4 日
I wonder why makedist doesn't support Uniform Distribution (Discrete).
But random can be called with that distribution.
N_clusters = 10;
rng('default');
idx = randi(N_clusters,N_clusters,1)
idx = 10×1
9 10 2 10 7 1 3 6 10 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
rng('default');
random('Discrete Uniform',N_clusters,N_clusters,1)
ans = 10×1
9 10 2 10 7 1 3 6 10 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 件のコメント
Snoopy
Snoopy 2025 年 8 月 4 日
This
idx = random('Discrete Uniform',N_clusters,[N_clusters 1]);
seems to work. I wonder about the makedist too. This caused the whole confusion on my end.

その他の回答 (1 件)

Matt J
Matt J 2025 年 8 月 4 日
編集済み: Matt J 2025 年 8 月 4 日
idx = floor( (N_clusters-1)*rand(N_clusters,1) )+1
  11 件のコメント
Snoopy
Snoopy 2025 年 8 月 4 日
Yes, that is the problem and hence I am seeking a solution to that, using still the random function.
Matt J
Matt J 2025 年 8 月 4 日
編集済み: Matt J 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))
idx = 10×1
9 10 2 10 7 1 3 6 10 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

This question is locked.

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

製品


リリース

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by