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

 Akzeptierte Antwort

Adam
Adam am 18 Aug. 2014
-rand

8 Kommentare

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.
Thankyou Adam it works
Thankyou Adam it works
Have you any idea of Yalmip?I stuck in that from 2 months and much worried
Yalmip is toolbox of Matalb
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Ben11
Ben11 am 18 Aug. 2014
Bearbeitet: Ben11 am 18 Aug. 2014
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).

Kategorien

Gefragt:

am 18 Aug. 2014

Kommentiert:

am 19 Aug. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by