Set specific standard deviation limit on randn matrix?

21 views (last 30 days)
I want to create a randn matrix, but I want all the values to be within 2 standard deviations away from the mean.
So I make the randn(a,b) matrix, but I'm confused as to how to set the limit of the standard deviation when creating the matrix itself.

Answers (3)

Wayne King
Wayne King on 9 Oct 2013
With the Statistics Toolbox:
pd = makedist('Normal','mu',0,'sigma',2);
t = truncate(pd,0,4);
% generate the random matrix - here 100x100
R = random(t,100,100);
The above truncates to [0,4], if you want [-2*sd,2*sd]
t = truncate(pd,-4,4);
R = random(t,100,100);
  6 Comments
Paul
Paul on 9 Oct 2013
Thanks again.
I'm just a little confused since the other poster said that it's not possible to create a randn matrix with limits on the SD, but that you would have to continue to resample until your conditions are met.
So when using truncate(pd,-2,2), my pd is a normal distribution, and then the -2 and 2 are the limits of distribution. Must a sigma value be provided before? Or is there a way to leave sigma as a variable.
Walter Roberson
Walter Roberson on 9 Oct 2013
Wayne used the new-fangled random() call, not randn().
Truncation is quite new, either this new release or the prior one if I recall.

Sign in to comment.


Walter Roberson
Walter Roberson on 9 Oct 2013
Are you aware that numbers generated by such a system will never be randomly distributed? The Normal distribution inherently requires infinite distribution.
If you generate by randn(), you can discard values whose abs() > 2, but beware that the result will not be normally distributed and will not have a standard distribution of 1.
  2 Comments
Paul
Paul on 9 Oct 2013
yep that makes sense, but I was hoping to just have no values in my "normal distribution" that were above the 2 SD.
I was initially going to use the abs() > 2 , but then would that leave me with missing values in my matrix? Is there a way to create the matrix with the limit set before, so the matrix that is created initially will have no values above 2 SD?
Walter Roberson
Walter Roberson on 9 Oct 2013
No, you cannot do that. You will need to keep creating values until you have enough for your purpose. For example instead of generating 10 values, generate 15, throw out the ones that are out of range, and see if you are left with at least 10; if not, then generate again, but if so then return the first 10.

Sign in to comment.


Roger Stafford
Roger Stafford on 9 Oct 2013
Edited: Roger Stafford on 9 Oct 2013
(Since you say 'randn' the mean will be zero and the standard deviation one.) You will have to call on 'randn' for more elements than you wish to have in your final matrix so as to select those which satisfy your condition.
x = [];
n = 0;
while n<a*b
t = randn(ceil(1.03*(a*b-n)),1);
x = [x;t(t<2)];
n = length(x);
end
x = reshape(x(1:a*b),a,b);
To not exceed two standard deviations if you use 1.03 above, you will usually have to go through the while-loop only once. With a looser condition, the multiplicative factor needs to be greater.
EDIT:
To accomplish this task for arbitrary mean, mu, and standard deviation sigma, replace the last line by:
x = sigma*reshape(x(1:a*b),a,b)+mu;

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!