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

 采纳的回答

-rand

8 个评论

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
What does YALMIP have to do with this question? Post your YALMIP questions on https://groups.google.com/forum/?fromgroups=#!forum/yalmip

请先登录,再进行评论。

更多回答(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).

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by