Random sequency generation at specific numbers
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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)))
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!