Random sequency generation at specific numbers

1 回表示 (過去 30 日間)
Guilherme Lopes de Campos
Guilherme Lopes de Campos 2023 年 9 月 20 日
回答済み: Steven Lord 2023 年 9 月 20 日
Dear community,
I would like to generate a sequency random at the follow vector:
Size : 1x19 double,
Then, I used the code:
V=DHSV; % the vector (size: 1x19)
N=5000; % total number wanted
RV=repmat(V,1,N/numel(V));
RV=RV(randperm(N));
Executing the code, the follow error message shown:
Error using repmat
Replication factors must be a row vector of integers or integer scalars.
Can help me, please?
Thank you very much,

採用された回答

Steven Lord
Steven Lord 2023 年 9 月 20 日
Replicate the vector so it has more elements than you want. Then use the two input form of randperm to select the desired number of elements from that replicated vector. Alternately if you're okay with selecting with more duplicate elements than would be in the replicated vector, use randi on the original vector.
x = 1:5;
n = 12; % Ask for 12 elements from x
replicatedX = repmat(x, 1, ceil(n/numel(x)))
replicatedX = 1×15
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
This randperm approach would select no more than 3 instances of each of the elements from 1 to 5, since the ceiling of 12/5 is 4.
y1 = replicatedX(randperm(numel(replicatedX), n))
y1 = 1×12
2 2 1 1 5 4 2 4 5 5 3 1
histogram(y1) % No bar is higher than 3
This randi approach could generate a vector with more than 3 of any elements but doesn't require the replicated vector.
y2 = x(randi(numel(x), 1, n))
y2 = 1×12
4 2 2 4 4 1 5 4 1 3 3 1
figure
histogram(y2) % Could have bins higher than 3

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by