Creating a random matrix with different probabilities
显示 更早的评论
Hey, I have 4 situations, and each situation has a different probability to be happened. P(0) = 0.2 P(1) = 0.46 P(2) = 0.16 P(3) = 0.18
I want to create a matrix(52,1) with [0 3] but with inequal probabilities. A= randi([0 3],52,1); With this, the probability to be have number 0,1,2,3 is 25%. Can u help me out?
Thanks in advance.
1 个评论
Stephen23
2018-6-1
A= randi([0 3],52,1);
limit=4;
W=zeros(size(A));
C=zeros(size(A));
k=1;
for i=1:length(A)
C(k)=C(k)+1;
if W(k)>=limit
k=k+1
end
W(k)=W(k)+A(i)
end
W(k+1:end)=[ ];
C(k+1:end)=[ ];
I want to know how many times the A exceed limit(4), but every time that exceed the limit for example A=5 i want to start with 5-4=1 over again, if A=6 6-4=2 etc. How can i do this one? Thanks in advance
采纳的回答
更多回答(1 个)
Steven Lord
2018-6-1
编辑:Steven Lord
2018-6-1
% Generate the vector of probabilities for each of your classes
p = [0.2 0.46 0.16 0.18];
% Cumulative probability vector
probabilityBins = cumsum([0 p]);
% Sample data
x = rand(1, 1e6);
% Bin each element of the sample data into the appropriate bin
% whose edges are in probabilityBins
D = discretize(x, probabilityBins, 0:3);
% Show the first 10 sample data points and their bins
[x(1:10); D(1:10)]
% Show that the probabilities are roughly what you'd expect
histogram(D, 'Normalization', 'probability')
% Turn on the grid to see how each bar matches its probability
yticks(sort(p))
grid on
I'd say that looks pretty good.
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!