how to random -1 and 1?
62 次查看(过去 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 个)
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!