필터 지우기
필터 지우기

how to random -1 and 1?

조회 수: 102 (최근 30일)
Tia
Tia 2013년 7월 27일
댓글: Stephen23 2023년 2월 8일
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

채택된 답변

Azzi Abdelmalek
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
Azzi Abdelmalek
Azzi Abdelmalek 2013년 7월 27일
Exact
Tia
Tia 2013년 7월 27일
ok, thank you for your advice

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Andrei Bobrov
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
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
James Tursa 2019년 4월 17일
Yes, the probability of getting -1 and +1 is 0.5

댓글을 달려면 로그인하십시오.


Mendi
Mendi 2020년 8월 19일
randsample([-1, 1],8,true)

Francesco Sgromo
Francesco Sgromo 2023년 2월 8일
편집: Francesco Sgromo 2023년 2월 8일
-1^(randi(2))
  댓글 수: 1
Stephen23
Stephen23 2023년 2월 8일
(-1).^randi(2,8,8)
ans = 8×8
1 1 1 -1 -1 1 -1 -1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 -1 1 1 -1 1 1 -1 1 -1 1 1 -1 -1 -1 1 -1 1 1 -1 -1 -1 -1 -1 1 1 -1 -1 1 -1 -1 -1 1 1 1 -1 1 -1 1 1 1 -1 -1 -1 -1 1

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by