Repeat numbers of X based on Y to produce Z

2 Ansichten (letzte 30 Tage)
JL
JL am 12 Aug. 2019
Kommentiert: Star Strider am 12 Aug. 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;]

Akzeptierte Antwort

Star Strider
Star Strider am 12 Aug. 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 Kommentare
JL
JL am 12 Aug. 2019
Thanks!
Star Strider
Star Strider am 12 Aug. 2019
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

John D'Errico
John D'Errico am 12 Aug. 2019
help repelem

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by