how to random -1 and 1?
이전 댓글 표시
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
채택된 답변
추가 답변 (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.
James Tursa
2019년 4월 17일
Yes, the probability of getting -1 and +1 is 0.5
Mendi
2020년 8월 19일
randsample([-1, 1],8,true)
Francesco Sgromo
2023년 2월 8일
편집: Francesco Sgromo
2023년 2월 8일
0 개 추천
-1^(randi(2))
댓글 수: 1
(-1).^randi(2,8,8)
카테고리
도움말 센터 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!