How do I get random variables generated from a distribution in [-1, 1]?

3 ビュー (過去 30 日間)
NOUFA 2023 年 5 月 28 日
コメント済み: NOUFA 2023 年 5 月 30 日
Hi Everyone, I have h represent the direct problem for the matrix (2N,1) , N=30,
I tried to get noise. It is required to multiply h by (1+0.01*\epsilon), where \epsilon is a random variables generated from a distribution in [-1, 1]. So I write the noise as
h.*(1+0.01*(2*rand(2*N,1)-1));
but i think there is something wrong, i attached the figure that represnt h with noise and without noise.
1 件のコメント表示 なし非表示 なし
Matt J 2023 年 5 月 28 日
We cannot see how the plot was generated.

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

採用された回答

Torsten 2023 年 5 月 28 日

The curve for the "noisy h" looks wrong although your formula for the "noisy h" is correct.
The noise is
h.*0.01*(2*rand(2*N,1)-1)
, the modified "noisy h" is
h + h.*0.01*(2*rand(2*N,1)-1) = h.*(1+0.01*(2*rand(2*N,1)-1))
2 件のコメント表示 1 件の古いコメント非表示 1 件の古いコメント
Torsten 2023 年 5 月 29 日
I don't know what your problem is. Maybe it's wrong because h is a row vector instead of a column vector as required ?
N = 30;
x = linspace(0,5,2*N).';
h = 0.1*sqrt(x);
h_noise = h.*(1+0.01*(2*rand(2*N,1)-1));
plot(x,[h,h_noise])
grid on

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

その他の回答 (1 件)

John D'Errico 2023 年 5 月 28 日
The MATLAB crystal ball is foggy today. You give us a tiny fragment of code, and then tell us that something strange happens. PROBABLE USER ERROR.
This fragment generates random numbers on the interval [-1,1].
2*rand(2*N,1)-1
But this fragment generates random numbers on a different interval:
h.*(1+0.01*(2*rand(2*N,1)-1))
What is that interval? Think about what multiplying a uniform random number does. Think about what adding a constant to that uniform number does.
For example, RAND generates numbers randomly and uniformly in the interval (0,1). Then think about what a*RAND does? It will now be uniform and random on the interval (0,a). Do you understand that?
Similarly, what does adding a constant do to rand? That is, a+rand will be uniform on the interval (a,1+a). Now look carefully at those expressions. Start with the first.
2*rand(2*N,1)-1
This maps rand first into the interval (0,2), then by subtracting 1, the result will be uniform on the interval (-1,1).
Now look at what you have. You multiply by 0.1, which maps that (-1,1) interval into (-0.01,0.01). Then you add 1. So now the random numbers are mapped into (0.99,1.01). Finally, you multiply by h. So the second code fragment generates uniform random numbers on the interval (0.99*h,1.01*h).
As for what you did wrong with those numbers, that is between you and your computer, since you have refrained from telling us anything at all.
1 件のコメント表示 なし非表示 なし
NOUFA 2023 年 5 月 30 日
I appreciated:)

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by