2011-05-15 83 views

回答

0

可以使用的hist可选的输出参数,像这样得到的随机数据的概率:

z=randn(10000,1); %# generate 10000 trials of a normally distributed random variable. 
[f,x]=hist(z,100); %# get x values and bin counts (f) 
prob=f/trapz(x,f); %# divide by area under the curve to get the 

您可以轻松地验证这给你的概率分布。

bar(x,prob);hold on 
plot(x,1/sqrt(2*pi)*exp(-(x.^2)/2),'r','linewidth',1.25);hold off 

enter image description here

您可以使用uitable上面的数据创建一个表。

data=num2cell([prob(:);x(:)]); 
colNames={'Probability','x'}; 
t=uitable('Data',data,'ColumnName',colNames); 

enter image description here

0

这可能是一个愚蠢的问题,但是,你有一个离散分布(二项分布,泊松分布,......)或连续分布的工作?如果您正在进行任何类型的连续分发,并添加一个步骤并将其表示为离散分布将导致麻烦。

即使您使用离散分布,表格表示也是不必要的步骤。

下面是一些代码,显示了一个非常简单的方法来做你想做的事情。

%% Parametric fitting, followed by random number generation 

% Generate some random data from a normal distribution with mean = 45 and 
% standard devation = 6 

X = 45 + 6 * randn(1000,1); 
foo = fitdist(X, 'normal') 

% Use the object to generate 1000 random numbers 

My_data = random(foo, 1000,1); 

mean(My_data) 
std(My_data) 


%% Kernel smoothing, followed by random number generation 

% Generate some random data 
X = 10 + 5 * randn(100,1); 
Y = 15 + 3 * randn(60,1); 
my_dist = vertcat(X,Y); 

% fit a distribution to the data 
bar = fitdist(my_dist, 'kernel') 

% generate 100 random numbers from the distribution 

random(bar, 100, 1) 

%% Fitting a discrete distribution 

% Use a poisson distribution to generate a 1000 random integers with mean = 6.8 

Z = poissrnd(6.8, 1000,1); 
foobar = fitdist(Z, 'poisson') 

% generate 100 random numbers from the distribution 
random(foobar, 100, 1)