生成一个只有0,1的向量,向量元素之和为一定值。
Show older comments
比如生成一个1行20列的向量,所有元素之和为5,且两个1之间至少有一个0。我现在只能实现前两步,最后如何保证两个1之间至少有一个0?请高手帮忙解答
A=zeros(1,20);
idx=randperm(20);
A(idx(1:5))=1;A(idx(6:20))=0;
A
Answers (0)
Categories
Find more on 随机数生成 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!