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

2 vues (au cours des 30 derniers jours)
NOUFA le 28 Mai 2023
Commenté : NOUFA le 30 Mai 2023
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 commentaireAfficher AucuneMasquer Aucune
Matt J le 28 Mai 2023
We cannot see how the plot was generated.

Connectez-vous pour commenter.

### Réponse acceptée

Torsten le 28 Mai 2023
Modifié(e) : Torsten le 28 Mai 2023
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 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Torsten le 29 Mai 2023
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 Connectez-vous pour commenter.

### Plus de réponses (1)

John D'Errico le 28 Mai 2023
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 commentaireAfficher AucuneMasquer Aucune
NOUFA le 30 Mai 2023
I appreciated:)

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!