2011-05-15 82 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)