Set specific standard deviation limit on randn matrix?

조회 수: 18 (최근 30일)
Paul
Paul 2013년 10월 9일
편집: Roger Stafford 2013년 10월 9일
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.

답변 (3개)

Wayne King
Wayne King 2013년 10월 9일
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
Paul
Paul 2013년 10월 9일
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 2013년 10월 9일
Wayne used the new-fangled random() call, not randn().
Truncation is quite new, either this new release or the prior one if I recall.

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


Walter Roberson
Walter Roberson 2013년 10월 9일
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
Paul
Paul 2013년 10월 9일
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 2013년 10월 9일
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.

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


Roger Stafford
Roger Stafford 2013년 10월 9일
편집: Roger Stafford 2013년 10월 9일
(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;

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by