how to random -1 and 1?
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (3)
Andrei Bobrov
le 27 Juil 2013
Modifié(e) : Andrei Bobrov
le 27 Juil 2013
2*randi([0 1],8)-1
or
2*(rand(8)>.5) - 1
2 commentaires
Ahteshamul Haq
le 17 Avr 2019
Is the probabilty of getting -1 and +1 is 0.5. If not, kindly suggest a way to get it.
James Tursa
le 17 Avr 2019
Yes, the probability of getting -1 and +1 is 0.5
Mendi
le 19 Août 2020
randsample([-1, 1],8,true)
Francesco Sgromo
le 8 Fév 2023
Modifié(e) : Francesco Sgromo
le 8 Fév 2023
0 votes
-1^(randi(2))
1 commentaire
(-1).^randi(2,8,8)
Catégories
En savoir plus sur Random Number Generation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!