Random Number from a given vector of numbers

9 vues (au cours des 30 derniers jours)
Israa Ahmed
Israa Ahmed le 26 Août 2021
Commenté : Arik Brown le 20 Avr 2024 à 0:45
Given a vector of certain allowed numbers,
how to generate another vector whose elements randomly take values from that vector of numbers?

Réponse acceptée

Stephen23
Stephen23 le 26 Août 2021
Modifié(e) : Stephen23 le 26 Août 2021
V = [-1,23,99,111,1024];
N = 3;
A = V(randi(numel(V),1,N)) % with repetition
A = 1×3
111 111 1024
B = V(randperm(numel(V),N)) % no repetition
B = 1×3
1024 111 23
  2 commentaires
Israa Ahmed
Israa Ahmed le 26 Août 2021
Thank you very much, it worked well.
Arik Brown
Arik Brown le 20 Avr 2024 à 0:45
Thank you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Tags

Produits


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by