how to random -1 and 1?
Show older comments
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
Accepted Answer
More Answers (3)
Andrei Bobrov
on 27 Jul 2013
Edited: Andrei Bobrov
on 27 Jul 2013
2*randi([0 1],8)-1
or
2*(rand(8)>.5) - 1
2 Comments
Ahteshamul Haq
on 17 Apr 2019
Is the probabilty of getting -1 and +1 is 0.5. If not, kindly suggest a way to get it.
James Tursa
on 17 Apr 2019
Yes, the probability of getting -1 and +1 is 0.5
Mendi
on 19 Aug 2020
randsample([-1, 1],8,true)
Francesco Sgromo
on 8 Feb 2023
Edited: Francesco Sgromo
on 8 Feb 2023
0 votes
-1^(randi(2))
1 Comment
(-1).^randi(2,8,8)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!