Random sequency generation at specific numbers
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Guilherme Lopes de Campos
am 20 Sep. 2023
Beantwortet: Steven Lord
am 20 Sep. 2023
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,
0 Kommentare
Akzeptierte Antwort
Steven Lord
am 20 Sep. 2023
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)))
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))
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))
figure
histogram(y2) % Could have bins higher than 3
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Random Number Generation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

