create Non-uniform distribution
11 次查看(过去 30 天)
显示 更早的评论
Hello!
I want to create non -uniform distribution using rand. for example, let X be random integer number with :
X=
0 w.p 1/3
1 w.p 1/2
2 w.p 1/6
meaning, with p =1/3 x is equal to 0, with p=1/2 x is equal to 1, and with p=1/6 x is equal to 2.
how can I set a series of Xi random numbers using rand (1,N)?
Thanks In Advance.
0 个评论
采纳的回答
更多回答(1 个)
Image Analyst
2015-12-26
Perhaps you mean randi() or randperm() instead of rand(). Anyway, here's one way of many:
N = 1000
% Assign numbers in the specified amounts.
% Assign the 0's.
X = zeros(1,N);
% Assign the 1's.
i1 = round(N/3+1)
i2 = i1 + round(N/2)
X(i1:i2) = 1;
% Assign the 2's.
X(i2+1:end) = 2;
% Now scramble them to give them a random order.
X = X(randperm(length(X)));
% Check distribution to verify.
counts = histogram(X, 'BinMethod', 'Integers', 'Normalization', 'pdf');
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!