Matlab command for generating random rational numbers between -1 and 0?

 Accepted Answer

-rand

8 Comments

if I am using "for loop" and for that I want to generate numbers that are in ascending order between [-1 0] then after half of my iterations I want to go back from 0 to -1 then what will be it's command?
a = -rand( 100, 1 );
a( 1:50 ) = sort( a( 1:50 ), 'ascend' );
a( 51:100 ) = sort( a( 51:100 ), 'descend' );
Something like that would do the job. I don't know if you need to use a for loop for other purposes, but if you don't then you can just create all your random numbers upfront like that and then sort them.
If you have to create them in a for loop then just assign them to a (preferably pre-sized) array in the for loop and then sort as above after the end of the for loop. 'ascend' is default sorting behaviour so you can omit that flag, I just included it for clarity.
Have you any idea of Yalmip?I stuck in that from 2 months and much worried
Sorry, I've never heard of it
What does YALMIP have to do with this question? Post your YALMIP questions on https://groups.google.com/forum/?fromgroups=#!forum/yalmip

Sign in to comment.

More Answers (1)

Like this?
a = -1;
b = 0;
r = a+(b-a)*rand(1,1)
If you want many of them change the inputs to rand. And if you want the fractional representation, use rats(r).

Asked:

on 18 Aug 2014

Commented:

on 19 Aug 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!