唯一的问题是我不知道如何设置比率.
解决方案该比率可以解释为确定性(您希望在每个1000个值的实现中都包含500个零和500个零)或>概率(一个实现可能比零多,但该比率保持着非常多的实现).
您的代码适用于概率解释,所以我假设您的意思是确定性的.在这种情况下,请以任意顺序将零和一值填充到 A 中(例如:首先是全1,然后是全零),然后应用随机置换:
length_sequence = 1000;比率= 0.5;%//解释为任何实现中必须存在的比例A =零(1,length_sequence);A(1:round(length_sequence * ratio))= 1;A = A(randperm(长度序列));I want to generate a random binary sequence with a ratio of 50%, that is, the same amounts of 0s and 1s. This is what I have so far:
length_sequence = 1000; ratio = 0.5; A = logical(randi([0 1],length_sequence,1));The only problem is that I dont know how to set the ratio.
解决方案The ratio can be interpreted as deterministic (you want 500 ones and 500 zeros in each realization of 1000 values) or as probabilistic (a realization may have more ones than zeros, but the ratio holds over a very large number of realizations).
Your code works for the probabilistic interpretation, so I'm assuming you mean the deterministic one. In that case, fill A with the zero and one values in any order (for example: first all ones, then all zeros), and then apply a random permutation:
length_sequence = 1000; ratio = 0.5; %// interpreted as proportion of ones that there must be in any realization A = zeros(1,length_sequence); A(1:round(length_sequence*ratio)) = 1; A = A(randperm(length_sequence));
更多推荐
生成具有特定比率的随机二进制序列
发布评论