Created random sequence based on predefined vector

1 vue (au cours des 30 derniers jours)
luca
luca le 12 Sep 2019
Commenté : madhan ravi le 12 Sep 2019
Given a vector
SP = [1 2 4 5 8 11 13 14 15 16 18 19 20];
I want to create random sequence based on the vector
Y = [4 24 35 8 9 20 42 40 12 13 49 22 32];
It means that in the random sequnce I want element one repeated 4 times, element 2 repeated 24 times, element 4 repeated 35 times, element 5 repeated 8 times, element 8 repeated 9 times and so on.
Hence, the length of the final sequence will be equal to the cumulative sum of Y
4 + 24 + 35 + 8 + 9 + 20 + 42 + 40 + 12 + 13 + 49 + 22 + 32 = 310

Réponse acceptée

madhan ravi
madhan ravi le 12 Sep 2019
Modifié(e) : madhan ravi le 12 Sep 2019
v = repelem(SP,Y);
W = v(randperm(numel(v))) % numel(W)
  2 commentaires
luca
luca le 12 Sep 2019
May you kindly explain how it works?
madhan ravi
madhan ravi le 12 Sep 2019
help repelem
help randperm

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by