フィルターのクリア

how to random -1 and 1?

59 ビュー (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by