Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


share|cite|improve this question
up vote 2 down vote accepted

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:

share|cite|improve this answer
@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


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


should give what you want.

share|cite|improve this answer
Many many thanks to you. And thanks to stack – srijan Aug 28 '12 at 14:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.