Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

hi, i have set of numbers as following :

1 vue (au cours des 30 derniers jours)
mohammed elmenshawy
mohammed elmenshawy le 12 Juil 2017
Clôturé : MATLAB Answer Bot le 20 Août 2021
Z=[1,5,7,4,3,6,77,88,34,73,58,97,23,55,30,49,86,76,64,64,54,54,65,76,76,62,83,59,62,57,48]; How to choose 10 random numbers from these numbers??
  1 commentaire
Star Strider
Star Strider le 12 Juil 2017
Essential duplicate of: i have the following array

Réponses (1)

Walter Roberson
Walter Roberson le 12 Juil 2017
Z(randperm(length(Z),10))
  5 commentaires
Walter Roberson
Walter Roberson le 12 Juil 2017
Breaking it up into steps:
random_index = randperm(length(Z));
Z(random_index(1:10))
That is, you start by taking a random permutation of the 31 possible indices into Z. Then you take 10 of those, so you get 10 unique indices into Z. Then you index Z at those locations to have chosen 10 random elements without replacement.
The code
Z(randperm(length(Z),10))
does this in one step.
Jan
Jan le 12 Juil 2017
@mohammed: You do not need:
Z = (randperm(length(Z), 10));
but:
SelectedZ = Z(randperm(length(Z), 10));

Cette question est clôturée.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by