Sign up ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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

2 Answers 2

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.