# Gaussian distribution with sum 1

43 views (last 30 days)
Felipe Bayona on 8 Dec 2020
Answered: John D'Errico on 11 Dec 2020
Hello Community
im trying to generate a 2D gaussian matrix, which the sum of all its values is equal to 1.
I mean, not distribuited around 1 (the center must be a value below 1 to achieve teh sum of all elements to be 1)
For sure Matlab must have already implemented something like that. Or, whats the mathematical approach I must use to develp my own function?
Thx a lot
##### 1 CommentShowHide None
John D'Errico on 11 Dec 2020
Please. It is a bad idea to accept your own answer when you don't understand what you are doing.

Felipe Bayona on 11 Dec 2020
I solved it dividing the sum value of the matrix minus 1, and dividing it by the number of elements in the matriz. The, i substract that value to each elemt of the matrix. The sum of the new matrix is 1
w = gausswin(7)
sw = (sum(w) -1) / numel(w);
nw = w - sw
sum(nw)
figure
plot(w)
hold on
plot(nw)
##### 1 CommentShowHide None
John D'Errico on 11 Dec 2020
You are making some comments which lead me to the conclusion you do not understand what a distribution is.
w = gausswin(7)
w = 7×1
0.0439 0.2494 0.7066 1.0000 0.7066 0.2494 0.0439
sw = (sum(w) -1) / numel(w);
nw = w - sw
nw = 7×1
-0.2418 -0.0363 0.4210 0.7143 0.4210 -0.0363 -0.2418
sum(nw)
ans = 1.0000
The vector nw is NOT a set of normally distributed random numbers!!!!! In fact, these are not random numbers at all.
For example, do you see that the vector is perfectly symmetrical around the center element? Is that a surprise to you?
So accepting your own answer here is a bad idea, if you don't know what you are doing.

### More Answers (2)

John D'Errico on 11 Dec 2020
Is the goal here to generate a set of random numbers which are apparently normally distributed, but with a sum of 1? Or is the goal to generate a set of numbers that have the SHAPE of a normal distribution PDF, yet have a sum of 1?
You seem to be asking for the former, yet your own accepted answer shows you generated a set of points off a Gaussian PDF, then shifted it so the sum was 1. As you should see, the result of what you did in your accepted answer is not a random set at all. A random set of numbers would not be perfectly symmetrical around the center element of the vector.
So IF you really want pseudo-random numbers with the property that the sum is 1, it is easy. Even trivially so. Use randn to generate a random vector.
n = 7;
X = randn(n,1);
sum(X)
ans = 3.0949
We should see the sum was not 1. We now just make it so.
X = X - mean(X) + 1/n
X = 7×1
0.5077 0.0421 -0.5125 -1.0100 1.4531 0.0371 0.4824
Are they now pseudo-normally distributed? Is the sum as desired?
sum(X)
ans = 1.0000
So the sum is now 1, and we have a vector which is indeed pseudo-randomly normally distributed. For a larger sample (so the histogram will look smooth), we can see it does indeed have the necessary properties.
n = 1e6;
X = randn(n,1);
X = X - mean(X) + 1/n;
sum(X)
ans = 1.0000
histogram(X,'norm','pdf') So while it is not clear what you are asking to do, but the title of your question was:
Gaussian distribution with sum 1
Your other comments also suggest that you may be confused as to what you are asking.

Ameer Hamza on 8 Dec 2020
M = randn(10);
M_sum1 = M./sum(M, 'all');
Check:
>> sum(M_sum1, 'all')
ans =
1
##### 1 CommentShowHide None
Felipe Bayona on 8 Dec 2020
Hi AMeer
Thx for your answer, but your division do not preserve the normal distribution of the values.
I found my own way:
I divided the sum - 1 of all values by the number of elements in the array, and then substract that factor to all of the elements in the array.
Check:
%Felipe
w = gausswin(7)
sw = (sum(w) -1) / numel(w);
nw = w - sw
sum(nw)
%Ameer
M_sum1 = w./sum(w, 'all')
sum(M_sum1)
figure
plot(w)
hold on
plot(nw)
plot(M_sum1)
legend(["W", "Felipe", "Ameer"])
Thanks for your help

R2020b

### Community Treasure Hunt

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

Start Hunting!