generate random number with total sum is 10

How can i generate a table of 6x2 with sum of the row is 10 for example:
0.6 0.4
0.5 0.5
0.7 0.3
i try this way:
r = rand(1, 3); % Start with 3 random numbers that don't sum to 1.
r = r / sum(r) % Normalize so the sum is 1.
however the sum is vertically. How i want to make the sum horizontally?
theSum = sum(r) % Check to make sure. Should be 1
tqvm

 채택된 답변

Bruno Luong
Bruno Luong 2021년 1월 13일
편집: Bruno Luong 2021년 1월 13일

1 개 추천

For the case of more generic sum of n variables Xi is equal to 1.
If the apriori distribution is supposed to be uniform density
Xi ~ U(0,1)
If the conditioning is
sum(Xi) = 1
we can show that the Xi conditional probability distribution has density of
pdf(Xi | sum Xi = 1) = (n-1)*(1-x)^(n-2).
Note that for n=2, the conditioning distribution happens to be a uniform distribution. This is not true of n > 2.
One neatly way to generate a proper conditioning distribution is using n independant exponential distribution then normalize by the sum
n = 4;
Y = -log(rand(100000,n));
X = Y ./ sum(Y,2); % Each row is a realization of the conditining
We can check it gives the expected conditioning distribution
x=linspace(0,1);
pdf=@(x)(n-1)*(1-x).^(n-2);
histogram(X(:),'Normalization','pdf');
hold on
plot(x,pdf(x),'MarkerSize',3);
title('exponential normamlization');
If you want further to constraint the Xi with in a lower and upper bounds, Roger Stafford's randfixedsum function does the job.

추가 답변 (2개)

James Tursa
James Tursa 2021년 1월 13일
편집: James Tursa 2021년 1월 13일

0 개 추천

Not sure from your wording if you want the row sums to be 1 or 10. If it is 1, then
r = rand(6,1);
r(:,2) = 1 - r(:,1);
Modifying your code you could also do this:
r = rand(6,2);
r = r ./ sum(r,2);

댓글 수: 3

Khairul nur
Khairul nur 2021년 1월 13일
many thanks...its work!
Bruno Luong
Bruno Luong 2021년 1월 13일
편집: Bruno Luong 2021년 1월 13일
Careful in selecting the methods if you need a correct conditioning distribution.
James's second method won't give a proper "uniform" conditioning distribution
r=rand(100000,2); r=r./sum(r,2);
histogram(r(:))
The first method gives a more "healty" distribution
r = rand(100000,1); r(:,2) = 1 - r(:,1);
histogram(r(:))
PS: This method won't extend if you need more than 2 that sum to 1. Need other methods for that case.
James Tursa
James Tursa 2021년 1월 13일
편집: James Tursa 2021년 1월 13일
@Bruno: Yes, the 2nd method posted was just to show OP how to modify his version of the code to get the row sums = 1. But if uniform distribution is desired then I would typically just point to Roger Stafford's randfixedsum solution as you have done. OP needs to pick a method for the distribution he wants.

댓글을 달려면 로그인하십시오.

Khairul nur
Khairul nur 2021년 2월 3일

0 개 추천

The first one works better, but if i change the matrix column into 3..the 2nd col become null or 0..how to fix this?

댓글 수: 4

You post in the wrong place (answer and not comment) so no one know what you refer to in "The first one".
opps..my bad.. the first one refer to as in above comments:
r = rand(6,1);
r(:,2) = 1 - r(:,1);
Bruno Luong
Bruno Luong 2021년 2월 3일
편집: Bruno Luong 2021년 2월 3일
Well you have been warned, I wrote in my comment;
"PS: This method won't extend if you need more than 2 that sum to 1. Need other methods for that case."
And I already give alternative method that can be extended in my answer using exponential pdf.
i already try it out..its working..tqvm

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2021년 1월 13일

댓글:

2021년 2월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by