从频率分布生成值

时间:2017-04-28 08:02:19

标签: python matlab frequency-distribution

我目前正在分析一个16位二进制字符串 - 类似于0010001010110100.我有大约30个字符串。我在Matlab编写了一个简单的程序,它计算所有30个字符串的每个位的1的数量。

所以,例如:

1 30

2 15

3 1

4 10

我想生成大致遵循上述频率分布的更多字符串(100s)。是否有Matlab(或Python或R)命令执行此操作?

我正在寻找的是这样的:http://www.prenhall.com/weiss_dswin/html/simulate.htm

1 个答案:

答案 0 :(得分:0)

在MATLAB中:只需在<上使用lt(或rand,小于):

len = 16; % string length
% counts of 1s for each bit (just random integer here)
counts = randi([0 30],[1 len]); 
% probability for 1 in each bit
prob = counts./30;
% generate 100 random strings 
n = 100;
moreStrings = rand(100,len);
% for each bit check if number is less than the probability of the bit
moreStrings = bsxfun(@lt, moreStrings, prob); % lt(x,y) := x < y

在Python中:

import numpy as np

len = 16 # string length
# counts of 1's for each bit (just random integer here)
counts = np.random.randint(0, 30, (1,16)).astype(float)
# probability for 1 in each bit
prob = counts/30
# generate 100 random strings 
n = 100
moreStrings = np.random.rand(100,len)
# for each bit check if number is less than the probability of the bit
moreStrings = moreStrings < prob