How can I generate a random vector between 1-9 with 10 numbers and at least one number should be >2.
Afficher commentaires plus anciens
X=rand(10,1)*(9-1)+1 How should I change this code to make at least one number >2.
Réponse acceptée
Plus de réponses (2)
One way,
X=rand(10,1)*(8-1)+1 +ceil(rand(10,1))
Azzi Abdelmalek
le 21 Déc 2014
X=rand(10,1)*8+1;
idx=X>2;
if nnz(idx)==0
X(randi(numel(X)))=7*rand+2;
end
1 commentaire
Hüseyin
le 21 Déc 2014
Catégories
En savoir plus sur Random Number Generation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!