how to create a random integernumbers with conditions?
2 次查看(过去 30 天)
显示 更早的评论
I just want to create an array at random which size will be 1 by sum([4 3 3]). The array contains 1 at 4 times, 2 at 3 times and 3 at 3 times. The result will be
A=[1 2 1 3 2 3 1 1 2 3], ie, random.
How can I generate it in the smart way?
0 个评论
采纳的回答
更多回答(1 个)
David Hill
2020-7-16
A=[4 3 3];
a=[];
for k=1:length(A)
a=[a,repmat(k,1,A(k))];
end
for k=1:5
a=a(randperm(length(a)));%you don't necessary have to do this loop, you could do it just once
end
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!