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

 採用された回答

Adam
Adam 2014 年 8 月 18 日

0 投票

-rand

8 件のコメント

sara
sara 2014 年 8 月 18 日
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?
Adam
Adam 2014 年 8 月 18 日
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.
sara
sara 2014 年 8 月 18 日
Thankyou Adam it works
sara
sara 2014 年 8 月 18 日
Thankyou Adam it works
sara
sara 2014 年 8 月 18 日
Have you any idea of Yalmip?I stuck in that from 2 months and much worried
sara
sara 2014 年 8 月 18 日
Yalmip is toolbox of Matalb
Adam
Adam 2014 年 8 月 18 日
Sorry, I've never heard of it
Johan Löfberg
Johan Löfberg 2014 年 8 月 19 日
What does YALMIP have to do with this question? Post your YALMIP questions on https://groups.google.com/forum/?fromgroups=#!forum/yalmip

サインインしてコメントする。

その他の回答 (1 件)

Ben11
Ben11 2014 年 8 月 18 日
編集済み: Ben11 2014 年 8 月 18 日

0 投票

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 ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2014 年 8 月 18 日

コメント済み:

2014 年 8 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by