Set specific standard deviation limit on randn matrix?
19 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (3 件)
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 件のコメント
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
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 件のコメント
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
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;
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Random Number Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!