# How to generate a $30\times30$ matrix $A$ where the elements of the matrix are randomly taken from $[-0.2, 0.2]$.

I am beginner in matlab.

I want to generate a a $30\times30$ matrix $A$ where the elements of the matrix are randomly taken from $[-0.2, 0.2]$.

I am not able to make code for this? Could anybody help me with this? Question might be very elementary.Please forgive me for that.

Thanks

-

To generate an $n \times m$ matrix with random entries from the interval $[a, b]$:

m = a + (b-a).*rand(n, m);

See the help page for rand: http://www.mathworks.com/help/techdoc/ref/rand.html

-
@jenifer Heartily thanks to you for your help. That is very helpful to me. –  srijan Aug 28 '12 at 14:08

To generate a 30x30 matrix of independent random numbers uniformly distributed on $[0,1]$, type

rand(30)


In your case you would like to have the numbers instead be uniformly distributed on $[-0.2,0.2]$, so you have to apply an appropriate linear transformation mapping $[0,1]$ onto $[-0.2,0.2]$. Adding and multiplying a matrix by scalars in MATLAB does these operations elementwise, so typing

0.4*(rand(30)-0.5)


should give what you want.

-
Many many thanks to you. And thanks to stack exchange.com. –  srijan Aug 28 '12 at 14:07