Repeat numbers of X based on Y to produce Z

2 visualizaciones (últimos 30 días)
JL
JL el 12 de Ag. de 2019
Comentada: Star Strider el 12 de Ag. de 2019
I have 2 matrices, X and Y, and using these 2 matrices I would like to produce a third one called Z.
X = [1; 2; 3; 4; 5;]
Y = [3; 1; 4; 2; 2;]
where in Z, the numbers in X are repeated based on X
Z = [1;
1;
1;
2;
3;
3;
3;
3;
4;
4;
5;
5;]

Respuesta aceptada

Star Strider
Star Strider el 12 de Ag. de 2019
Use the repelem function (R2015a and later):
Z = repelem(X, Y)
producing:
Z =
1
1
1
2
3
3
3
3
4
4
5
5
  2 comentarios
JL
JL el 12 de Ag. de 2019
Thanks!
Star Strider
Star Strider el 12 de Ag. de 2019
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

John D'Errico
John D'Errico el 12 de Ag. de 2019
help repelem

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by