Azzera filtri
Azzera filtri

how can I fill a matrix with randomly -0.01 and 0.01

1 visualizzazione (ultimi 30 giorni)
I want to fill a matrix with only two diffrent numbers e.g. 0.01 and -0.01.

Risposta accettata

Stephen23
Stephen23 il 23 Mar 2017
Modificato: Stephen23 il 23 Mar 2017
>> V = [0.01,-0.01];
>> V(randi(numel(V),5,5))
ans =
-0.010000 0.010000 -0.010000 0.010000 0.010000
-0.010000 0.010000 0.010000 0.010000 -0.010000
-0.010000 -0.010000 0.010000 0.010000 -0.010000
0.010000 -0.010000 -0.010000 -0.010000 -0.010000
-0.010000 0.010000 -0.010000 0.010000 -0.010000
Although you could calculate the values from randi, this method using indexing has the advantage that the values are not calculated but are exactly those values that you define in V.
  1 Commento
Simon Blum
Simon Blum il 23 Mar 2017
Thanks Stephen. I tried it the following way, but obviously your solution is much better:
V = (2* randi(2,[5,5]) - 3)/100;

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices 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!

Translated by