Repeat numbers of X based on Y to produce Z

2 views (last 30 days)
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;]

Accepted Answer

Star Strider
Star Strider on 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

More Answers (1)

John D'Errico
John D'Errico on 12 Aug 2019
help repelem

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!