generate (not round) 2 decimal numbers

1 visualizzazione (ultimi 30 giorni)
P K
P K il 14 Set 2016
Commentato: Walter Roberson il 14 Set 2016
Hello, in the first part of more complicated calculation I need some restriction to "rand" or so. My point is a reduction of unnecessary calculations and occurence of crowding results by simplification of the random number choice to 2 decimals in the first place. Is it possible?
Thank you
  3 Commenti
P K
P K il 14 Set 2016
Modificato: P K il 14 Set 2016
Even if done in parts I am dealing with bigger set of numbers, if you imagine the set it selects from, it is better to start with 2decimal numbers than 10decimal or what "rand" realy does. When I put in 1e7 Xs to not run out of RAM, it just does not works for me well now
José-Luis
José-Luis il 14 Set 2016
The you need to use a format other than double. I am afraid "reducing" the number of decimals will do nothing for you, the number will still be stored as a double and will continue to use the same amount of memory.
Please explain what you are trying to achieve in order to help you better.

Accedi per commentare.

Risposte (2)

KSSV
KSSV il 14 Set 2016
Modificato: KSSV il 14 Set 2016
Are you looking for something like this?
sprintf('%3.2f',rand(1,1))

Stephen23
Stephen23 il 14 Set 2016
Modificato: Stephen23 il 14 Set 2016
Here are two methods of providing random numbers rounded to two decimal places:
>> randi([0,100],5)/100
ans =
0.8200 0.0900 0.1500 0.1400 0.6600
0.9100 0.2800 0.9800 0.4200 0.0300
0.1200 0.5500 0.9600 0.9200 0.8500
0.9200 0.9600 0.4900 0.8000 0.9400
0.6300 0.9700 0.8000 0.9600 0.6800
>> round(100*rand(5))/100
ans =
0.7600 0.7100 0.8200 0.4400 0.4900
0.7400 0.0300 0.6900 0.3800 0.4500
0.3900 0.2800 0.3200 0.7700 0.6500
0.6600 0.0500 0.9500 0.8000 0.7100
0.1700 0.1000 0.0300 0.1900 0.7500
Note that you also need to understand the behaviors of floating point numbers:
  1 Commento
Walter Roberson
Walter Roberson il 14 Set 2016
Note: if memory space is a problem then you can use
randi([0,100], 5, 'uint8')
and adjust the rest of your algorithm to expect these to be 100 times too large. It probably will not help much in practice... it will probably lead to more problems than it is worth.

Accedi per commentare.

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