making an array to simulate states of a paramagnet with values of -1,+1.
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Tom Edwards
il 12 Ott 2019
Risposto: Jos (10584)
il 12 Ott 2019
I want to make an array containing only the values 1 and -1. It should be randomly generated so as to get a random number of occurences of both values.
I made the following but I feel like its way too complicated. Is there a simpler way? thanks
x = randi ([-1,1],1,n) ;
for k = 1 : n
if x (k) == 0
r = rand ;
if r > 0.5
x(k) = 1 ;
else
x(k) = -1;
end
end
end
0 Commenti
Risposta accettata
Jos (10584)
il 12 Ott 2019
Create a random vector with two values and map those to -1 and 1. An easy solution (with n=10):
x = 2 * randi([0 1], 1, 10) - 1
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Thermodynamics and Heat Transfer in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!