Generate random numbers with specific properties

1 回表示 (過去 30 日間)
Dr. Seis
Dr. Seis 2012 年 3 月 12 日
Is anyone aware of something within the Statistics Toolbox (or an FEX submission) that can generate a set of M random numbers where the mean of the random set is X and the mean of the absolute value of the random set is Y.
For example:
M = 21;
X = 0;
Y = 0.5;
A = some_rand_function(M,X,Y)
For example, a non-random version of A that almost matches the specific criteria is:
>> A = -1:0.1:1;
>> mean(A)
ans =
0
>> mean(abs(A))
ans =
0.528

採用された回答

Tom Lane
Tom Lane 2012 年 3 月 14 日
Not all combinations (X,Y) will work of course. If you do not need a theoretical answer, and are content with something that might work, consider generating a sample any way you want, then trying to adjust it to fit your constraints:
>> X = 5;
>> Y = 7;
>> z = randn(100,1);
>> a = fminsearch(@(a) (X-mean(a(1)+a(2)*z))^2 + (Y-mean(abs(a(1)+a(2)*z)))^2,[5 5])
a =
5.3484 7.4122
>> mean(a(1)+a(2)*z)
ans =
5.0000
>> mean(abs(a(1)+a(2)*z))
ans =
7.0000
  1 件のコメント
Dr. Seis
Dr. Seis 2012 年 3 月 14 日
As one of my professors used to say, "Good enough for government work!"
Thanks, Tom!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by