how to random -1 and 1?
조회 수: 102 (최근 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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!