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

355 Views Asked by At

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

2

There are 2 best solutions below

1
On BEST ANSWER

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

1
On

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.