random integer condition selection
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
abdul rehman
le 10 Juil 2020
Commenté : John D'Errico
le 10 Juil 2020
r1 = rand((randi(10)),1);
from these 10 random values how can I select only those that are > than 0.5
0 commentaires
Réponse acceptée
Plus de réponses (1)
John D'Errico
le 10 Juil 2020
You want to generate a random number of random numbers? With an additional constraint that all are greater than 0.5? This is far esier than you think. (You had an unnecessary pair of parens in your code.)
r1 = rand(randi(10),1);
So that generate a random length vector of uniform random numbers between 0 and 1. But if you wanted to have all of them be numbers between .5 and 1, this is easy.
r1 = (1 + rand(randi(10),1))/2;
So adding 1 to a number between 0 and 1 makes it lie between 1 and 2. If you then divide by 2, it will fall always betweem .5 and 1.
The result is a vector of random length, where all lie between .5 and 1.
2 commentaires
John D'Errico
le 10 Juil 2020
Your stated final requirement is the result must lie between .5 and 1. I showed you how to do that.
Voir également
Catégories
En savoir plus sur Random Number Generation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!