how to random -1 and 1?
32 ビュー (過去 30 日間)
古いコメントを表示
m=round(2*rand(8)-1)
the output:
m =
1 0 0 1 0 0 0 -1
1 1 0 0 1 1 0 -1
0 1 -1 0 0 0 1 1
1 0 0 1 0 0 1 0
0 0 -1 1 1 -1 1 1
-1 -1 -1 1 -1 0 0 -1
-1 -1 1 -1 0 -1 1 0
0 -1 0 1 1 0 0 0
but i don't want zero's output. can anyone help me clear this? thanks
0 件のコメント
採用された回答
Azzi Abdelmalek
2013 年 7 月 27 日
編集済み: Azzi Abdelmalek
2013 年 7 月 27 日
m=randi(2,8)-1
m(~m)=-1
%or
m=randi([-1 ,1],8)
m(~m)=-1
%or
m=fix(rand(8)+0.5)
m(~m)=-1
3 件のコメント
その他の回答 (3 件)
Andrei Bobrov
2013 年 7 月 27 日
編集済み: Andrei Bobrov
2013 年 7 月 27 日
2*randi([0 1],8)-1
or
2*(rand(8)>.5) - 1
2 件のコメント
Ahteshamul Haq
2019 年 4 月 17 日
Is the probabilty of getting -1 and +1 is 0.5. If not, kindly suggest a way to get it.
参考
カテゴリ
Help Center および File Exchange で Random Number Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!