How can I randomize target positions in a Gaussian distribution?

1 回表示 (過去 30 日間)
Mint
Mint 2021 年 11 月 30 日
編集済み: dpb 2021 年 12 月 1 日
I would like to determine several target positions in the Z direction (height). 10 values are to be determined, Gaussian distributed around the value 80, with a standard deviation of 40 and a repetition of the drawing for a value of <0 and >160. I thank you for any help and suggestions!

採用された回答

dpb
dpb 2021 年 11 月 30 日
編集済み: dpb 2021 年 12 月 1 日
Z=randn(10,1)*40+80;
isOK=all(iswithin(Z,0,160));
while ~isOK
% regenerate code here depending on what your definition of "regeneration" is
Z=randn(10,1)*40+80;
isOK=all(iswithin(Z,0,160));
end
The above regenerates a whole new sample; with these parameters the frequency of failing the test is only 5%.
If the number of trials gets to be extremely large this might be an observable slowdown, but for simplicity it's hard to beat other than just truncating the values outside the range.
Oh... iswithin is my "syntactic sugar" routine
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
>>

その他の回答 (0 件)

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by