Take the 2-minute tour ×
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.

Thanks

share|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: http://www.mathworks.com/help/techdoc/ref/rand.html

share|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

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.

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

Your Answer

 
discard

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.