random number generation with changing probability

3 次查看(过去 30 天)
hi all,
I want to generate either 0 or 1 randomly 50 times but with changing probability for 1 and 0. For example if i do 50 trails then for the first 10 trails 1 should definitely come means probability of occurrence of 1 is 1 and 0 is 0, for next 10 trails probability of occurrence of 1 becomes 0.75 and 0 becomes 0.25 means for this 10 trails 1 should come more often that 0, for next 30 trails probability of occurrence of 1 becomes 0.5 and 0 becomes 0.5. Can anyone suggest me how to do this ??

采纳的回答

Fangjun Jiang
Fangjun Jiang 2011-8-26
a=randsrc(10,1,[1 0;1 0]);
b=randsrc(10,1,[1 0;.75 .25]);
c=randsrc(30,1,[1 0; .5 .5]);
data=[a; b; c]

更多回答(2 个)

Sean de Wolski
Sean de Wolski 2011-8-26
This should do it:
function X = probVals(sz,vals,probs)
%Creates a matrix of values occurring at specified probabilities
%SCd 08/26/2011
%
% sz: size vector
% vals: values
% probs: probabilities (must sum to 1)
%
%Error checking
assert(nargin==3,'Three input arguments expected');
assert(isequal(size(vals),size(probs)),'vals and probs are expected to be the same size');
assert(all(probs>=0&probs<=1),'probs are expected to lie on the interval [0 1]');
assert(abs(1-sum(probs))<10^-6,'probs are expected to sum to 1');
%Engine:
probs = cumsum(probs);
X = rand(sz);
[jnk,bin] = histc(X,[-inf,(probs(:)'),inf]);
X = vals(bin);

Lucas García
Lucas García 2011-8-26
If I understand correctly, you are looking for a random number generation of the binomial distribution, your outcome can either be 0 or 1. You can use the Statistics Toolbox for this:
N = [10,10,30];
P = 1:-0.25:0.5;
R = binornd(N,P);
You obtain in R the total number of ones for each N(i) trials with P(i) probability. The total number of zeros will be N-R.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by