how to randomly vary 2 numbers?
Ältere Kommentare anzeigen
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
Weitere Antworten (2)
Andrei Bobrov
am 5 Jul. 2012
Bearbeitet: Andrei Bobrov
am 5 Jul. 2012
eg
A = (rand(5) > .5) + 0;
A(~A) = -1;
or
A = 2*randi([0 1],30,1) - 1;
1 Kommentar
Isabel
am 5 Jul. 2012
Orazio Sorgonà
am 2 Apr. 2022
I use
A = -1^(randi([1,2]));
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!