MATLAB Answers


generate random negative numbers

Asked by PK
on 26 Jan 2013
Latest activity Commented on by Edgar Andres Parra Rucaurte about 3 hours ago
Accepted Answer by Matt J

I thank if some one can help in this, how can i generate a matrix of some size for ex 100x100 with in the range of [-0.01,0] and that too with decimal values contained in it like -0.001,-0.0056 like that and also for the ranges like [-60,-30] with decimal values like -58.254,-56.985 and only 3 decimal values after the point.


The answer to this is the example to rand() in the help.

on 28 Jan 2013

@ Image Analyst no rand did not work for negative numbers.

Sorry but you are wrong. Look at Greg's code below, which is the same as the help example I referred you to. You can certainly have "a" and "b" be negative numbers. Why do you think you can't? No one said they had to be positive.


No products are associated with this question.

2 Answers

Answer by Matt J
on 28 Jan 2013
 Accepted answer

 n=3; %number of decimal places
 A = randi([-60,-30]*10^n,[100,100])/10^n;


on 28 Jan 2013

Super answer Matt J

thanks this was helpful!!

Answer by Greg Heath
on 26 Jan 2013

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

Hope this helps.

Thank you for formally accepting my answer.



Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!