Hi! I want to generate a column of 30 randomly varying -1s and 1s. How can I do that? I was trying randi([-1:2:1],30,1) but that gives me zeros too and I don't want zeros, only -1 and 1. Thanks

 Akzeptierte Antwort

John Petersen
John Petersen am 5 Jul. 2012

1 Stimme

x = 2*round(rand(30,1)) - 1;

3 Kommentare

Isabel
Isabel am 5 Jul. 2012
This gives me a column of zeros only. Thanks anyways
John Petersen
John Petersen am 5 Jul. 2012
Bearbeitet: John Petersen am 5 Jul. 2012
No it doesn't. Maybe you didn't grab the whole line?
Isabel
Isabel am 5 Jul. 2012
Sorry, it does work, my bad. I think I din't copy the whole formula or something. Thank you very much for your answer!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Andrei Bobrov
Andrei Bobrov am 5 Jul. 2012
Bearbeitet: Andrei Bobrov am 5 Jul. 2012

1 Stimme

eg
A = (rand(5) > .5) + 0;
A(~A) = -1;
or
A = 2*randi([0 1],30,1) - 1;

Kategorien

Mehr zu Random Number Generation finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by