Borrar filtros
Borrar filtros

Create a random sequence with specified elements

3 visualizaciones (últimos 30 días)
luca
luca el 2 de Ag. de 2019
Respondida: madhan ravi el 2 de Ag. de 2019
Hi
Given the following vector
Q= [25 30 35 30 40 20 25 30]
I want to create a random vector S that contain the
Element 1, 25 times
Element 2, 30 times
Element 3, 35 times
Element 4, 30 times
Element 5, 40 times
Element 6, 20 times
Element 7, 25 times
Element 8, 30 times
such as
S= [1 8 3 4 5 6 7 8 1 2 3 5 7 4 2 2 5 7 ...]
The creation of S should be random. but the number of times an element is repetead should respect the quantity indicated in Q.

Respuesta aceptada

madhan ravi
madhan ravi el 2 de Ag. de 2019
s = repelem(1:numel(Q),Q);
S = s(randperm(numel(s)))

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by